| Line Number |
../DebugInfoTest/example_mips_dbg.ll
BUT NOT
../DebugInfoTest/example_mips.ll
|
Line Number |
../DebugInfoTest/example_mips.ll
BUT NOT
../DebugInfoTest/example_mips_dbg.ll
|
| 1 |
//===- ValueTracking.cpp - Walk computations to compute properties --------===// |
1 |
//===- ValueTracking.cpp - Walk computations to compute properties --------===// |
| 2 |
// |
2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
8 |
// |
| 9 |
// This file contains routines that help analyze properties that chains of |
9 |
// This file contains routines that help analyze properties that chains of |
| 10 |
// computations have. |
10 |
// computations have. |
| 11 |
// |
11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
13 |
|
| 14 |
#include "llvm/Analysis/ValueTracking.h" |
14 |
#include "llvm/Analysis/ValueTracking.h" |
| 15 |
#include "llvm/ADT/APFloat.h" |
15 |
#include "llvm/ADT/APFloat.h" |
| 16 |
#include "llvm/ADT/APInt.h" |
16 |
#include "llvm/ADT/APInt.h" |
| 17 |
#include "llvm/ADT/ArrayRef.h" |
17 |
#include "llvm/ADT/ArrayRef.h" |
| 18 |
#include "llvm/ADT/STLExtras.h" |
18 |
#include "llvm/ADT/STLExtras.h" |
| 19 |
#include "llvm/ADT/ScopeExit.h" |
19 |
#include "llvm/ADT/ScopeExit.h" |
| 20 |
#include "llvm/ADT/SmallPtrSet.h" |
20 |
#include "llvm/ADT/SmallPtrSet.h" |
| 21 |
#include "llvm/ADT/SmallSet.h" |
21 |
#include "llvm/ADT/SmallSet.h" |
| 22 |
#include "llvm/ADT/SmallVector.h" |
22 |
#include "llvm/ADT/SmallVector.h" |
| 23 |
#include "llvm/ADT/StringRef.h" |
23 |
#include "llvm/ADT/StringRef.h" |
| 24 |
#include "llvm/ADT/iterator_range.h" |
24 |
#include "llvm/ADT/iterator_range.h" |
| 25 |
#include "llvm/Analysis/AliasAnalysis.h" |
25 |
#include "llvm/Analysis/AliasAnalysis.h" |
| 26 |
#include "llvm/Analysis/AssumeBundleQueries.h" |
26 |
#include "llvm/Analysis/AssumeBundleQueries.h" |
| 27 |
#include "llvm/Analysis/AssumptionCache.h" |
27 |
#include "llvm/Analysis/AssumptionCache.h" |
| 28 |
#include "llvm/Analysis/ConstantFolding.h" |
28 |
#include "llvm/Analysis/ConstantFolding.h" |
| 29 |
#include "llvm/Analysis/GuardUtils.h" |
29 |
#include "llvm/Analysis/GuardUtils.h" |
| 30 |
#include "llvm/Analysis/InstructionSimplify.h" |
30 |
#include "llvm/Analysis/InstructionSimplify.h" |
| 31 |
#include "llvm/Analysis/Loads.h" |
31 |
#include "llvm/Analysis/Loads.h" |
| 32 |
#include "llvm/Analysis/LoopInfo.h" |
32 |
#include "llvm/Analysis/LoopInfo.h" |
| 33 |
#include "llvm/Analysis/OptimizationRemarkEmitter.h" |
33 |
#include "llvm/Analysis/OptimizationRemarkEmitter.h" |
| 34 |
#include "llvm/Analysis/TargetLibraryInfo.h" |
34 |
#include "llvm/Analysis/TargetLibraryInfo.h" |
| 35 |
#include "llvm/Analysis/VectorUtils.h" |
35 |
#include "llvm/Analysis/VectorUtils.h" |
| 36 |
#include "llvm/IR/Argument.h" |
36 |
#include "llvm/IR/Argument.h" |
| 37 |
#include "llvm/IR/Attributes.h" |
37 |
#include "llvm/IR/Attributes.h" |
| 38 |
#include "llvm/IR/BasicBlock.h" |
38 |
#include "llvm/IR/BasicBlock.h" |
| 39 |
#include "llvm/IR/Constant.h" |
39 |
#include "llvm/IR/Constant.h" |
| 40 |
#include "llvm/IR/ConstantRange.h" |
40 |
#include "llvm/IR/ConstantRange.h" |
| 41 |
#include "llvm/IR/Constants.h" |
41 |
#include "llvm/IR/Constants.h" |
| 42 |
#include "llvm/IR/DerivedTypes.h" |
42 |
#include "llvm/IR/DerivedTypes.h" |
| 43 |
#include "llvm/IR/DiagnosticInfo.h" |
43 |
#include "llvm/IR/DiagnosticInfo.h" |
| 44 |
#include "llvm/IR/Dominators.h" |
44 |
#include "llvm/IR/Dominators.h" |
| 45 |
#include "llvm/IR/EHPersonalities.h" |
45 |
#include "llvm/IR/EHPersonalities.h" |
| 46 |
#include "llvm/IR/Function.h" |
46 |
#include "llvm/IR/Function.h" |
| 47 |
#include "llvm/IR/GetElementPtrTypeIterator.h" |
47 |
#include "llvm/IR/GetElementPtrTypeIterator.h" |
| 48 |
#include "llvm/IR/GlobalAlias.h" |
48 |
#include "llvm/IR/GlobalAlias.h" |
| 49 |
#include "llvm/IR/GlobalValue.h" |
49 |
#include "llvm/IR/GlobalValue.h" |
| 50 |
#include "llvm/IR/GlobalVariable.h" |
50 |
#include "llvm/IR/GlobalVariable.h" |
| 51 |
#include "llvm/IR/InstrTypes.h" |
51 |
#include "llvm/IR/InstrTypes.h" |
| 52 |
#include "llvm/IR/Instruction.h" |
52 |
#include "llvm/IR/Instruction.h" |
| 53 |
#include "llvm/IR/Instructions.h" |
53 |
#include "llvm/IR/Instructions.h" |
| 54 |
#include "llvm/IR/IntrinsicInst.h" |
54 |
#include "llvm/IR/IntrinsicInst.h" |
| 55 |
#include "llvm/IR/Intrinsics.h" |
55 |
#include "llvm/IR/Intrinsics.h" |
| 56 |
#include "llvm/IR/IntrinsicsAArch64.h" |
56 |
#include "llvm/IR/IntrinsicsAArch64.h" |
| 57 |
#include "llvm/IR/IntrinsicsAMDGPU.h" |
57 |
#include "llvm/IR/IntrinsicsAMDGPU.h" |
| 58 |
#include "llvm/IR/IntrinsicsRISCV.h" |
58 |
#include "llvm/IR/IntrinsicsRISCV.h" |
| 59 |
#include "llvm/IR/IntrinsicsX86.h" |
59 |
#include "llvm/IR/IntrinsicsX86.h" |
| 60 |
#include "llvm/IR/LLVMContext.h" |
60 |
#include "llvm/IR/LLVMContext.h" |
| 61 |
#include "llvm/IR/Metadata.h" |
61 |
#include "llvm/IR/Metadata.h" |
| 62 |
#include "llvm/IR/Module.h" |
62 |
#include "llvm/IR/Module.h" |
| 63 |
#include "llvm/IR/Operator.h" |
63 |
#include "llvm/IR/Operator.h" |
| 64 |
#include "llvm/IR/PatternMatch.h" |
64 |
#include "llvm/IR/PatternMatch.h" |
| 65 |
#include "llvm/IR/Type.h" |
65 |
#include "llvm/IR/Type.h" |
| 66 |
#include "llvm/IR/User.h" |
66 |
#include "llvm/IR/User.h" |
| 67 |
#include "llvm/IR/Value.h" |
67 |
#include "llvm/IR/Value.h" |
| 68 |
#include "llvm/Support/Casting.h" |
68 |
#include "llvm/Support/Casting.h" |
| 69 |
#include "llvm/Support/CommandLine.h" |
69 |
#include "llvm/Support/CommandLine.h" |
| 70 |
#include "llvm/Support/Compiler.h" |
70 |
#include "llvm/Support/Compiler.h" |
| 71 |
#include "llvm/Support/ErrorHandling.h" |
71 |
#include "llvm/Support/ErrorHandling.h" |
| 72 |
#include "llvm/Support/KnownBits.h" |
72 |
#include "llvm/Support/KnownBits.h" |
| 73 |
#include "llvm/Support/MathExtras.h" |
73 |
#include "llvm/Support/MathExtras.h" |
| 74 |
#include |
74 |
#include |
| 75 |
#include |
75 |
#include |
| 76 |
#include |
76 |
#include |
| 77 |
#include |
77 |
#include |
| 78 |
#include |
78 |
#include |
| 79 |
|
79 |
|
| 80 |
using namespace llvm; |
80 |
using namespace llvm; |
| 81 |
using namespace llvm::PatternMatch; |
81 |
using namespace llvm::PatternMatch; |
| 82 |
|
82 |
|
| 83 |
// Controls the number of uses of the value searched for possible |
83 |
// Controls the number of uses of the value searched for possible |
| 84 |
// dominating comparisons. |
84 |
// dominating comparisons. |
| 85 |
static cl::opt DomConditionsMaxUses("dom-conditions-max-uses", |
85 |
static cl::opt DomConditionsMaxUses("dom-conditions-max-uses", |
| 86 |
cl::Hidden, cl::init(20)); |
86 |
cl::Hidden, cl::init(20)); |
| 87 |
|
87 |
|
| 88 |
|
88 |
|
| 89 |
/// Returns the bitwidth of the given scalar or pointer type. For vector types, |
89 |
/// Returns the bitwidth of the given scalar or pointer type. For vector types, |
| 90 |
/// returns the element type's bitwidth. |
90 |
/// returns the element type's bitwidth. |
| 91 |
static unsigned getBitWidth(Type *Ty, const DataLayout &DL) { |
91 |
static unsigned getBitWidth(Type *Ty, const DataLayout &DL) { |
| 92 |
if (unsigned BitWidth = Ty->getScalarSizeInBits()) |
92 |
if (unsigned BitWidth = Ty->getScalarSizeInBits()) |
| 93 |
return BitWidth; |
93 |
return BitWidth; |
| 94 |
|
94 |
|
| 95 |
return DL.getPointerTypeSizeInBits(Ty); |
95 |
return DL.getPointerTypeSizeInBits(Ty); |
| 96 |
} |
96 |
} |
| 97 |
|
97 |
|
| 98 |
// Given the provided Value and, potentially, a context instruction, return |
98 |
// Given the provided Value and, potentially, a context instruction, return |
| 99 |
// the preferred context instruction (if any). |
99 |
// the preferred context instruction (if any). |
| 100 |
static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) { |
100 |
static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) { |
| 101 |
// If we've been provided with a context instruction, then use that (provided |
101 |
// If we've been provided with a context instruction, then use that (provided |
| 102 |
// it has been inserted). |
102 |
// it has been inserted). |
| 103 |
if (CxtI && CxtI->getParent()) |
103 |
if (CxtI && CxtI->getParent()) |
| 104 |
return CxtI; |
104 |
return CxtI; |
| 105 |
|
105 |
|
| 106 |
// If the value is really an already-inserted instruction, then use that. |
106 |
// If the value is really an already-inserted instruction, then use that. |
| 107 |
CxtI = dyn_cast(V); |
107 |
CxtI = dyn_cast(V); |
| 108 |
if (CxtI && CxtI->getParent()) |
108 |
if (CxtI && CxtI->getParent()) |
| 109 |
return CxtI; |
109 |
return CxtI; |
| 110 |
|
110 |
|
| 111 |
return nullptr; |
111 |
return nullptr; |
| 112 |
} |
112 |
} |
| 113 |
|
113 |
|
| 114 |
static const Instruction *safeCxtI(const Value *V1, const Value *V2, const Instruction *CxtI) { |
114 |
static const Instruction *safeCxtI(const Value *V1, const Value *V2, const Instruction *CxtI) { |
| 115 |
// If we've been provided with a context instruction, then use that (provided |
115 |
// If we've been provided with a context instruction, then use that (provided |
| 116 |
// it has been inserted). |
116 |
// it has been inserted). |
| 117 |
if (CxtI && CxtI->getParent()) |
117 |
if (CxtI && CxtI->getParent()) |
| 118 |
return CxtI; |
118 |
return CxtI; |
| 119 |
|
119 |
|
| 120 |
// If the value is really an already-inserted instruction, then use that. |
120 |
// If the value is really an already-inserted instruction, then use that. |
| 121 |
CxtI = dyn_cast(V1); |
121 |
CxtI = dyn_cast(V1); |
| 122 |
if (CxtI && CxtI->getParent()) |
122 |
if (CxtI && CxtI->getParent()) |
| 123 |
return CxtI; |
123 |
return CxtI; |
| 124 |
|
124 |
|
| 125 |
CxtI = dyn_cast(V2); |
125 |
CxtI = dyn_cast(V2); |
| 126 |
if (CxtI && CxtI->getParent()) |
126 |
if (CxtI && CxtI->getParent()) |
| 127 |
return CxtI; |
127 |
return CxtI; |
| 128 |
|
128 |
|
| 129 |
return nullptr; |
129 |
return nullptr; |
| 130 |
} |
130 |
} |
| 131 |
|
131 |
|
| 132 |
static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, |
132 |
static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, |
| 133 |
const APInt &DemandedElts, |
133 |
const APInt &DemandedElts, |
| 134 |
APInt &DemandedLHS, APInt &DemandedRHS) { |
134 |
APInt &DemandedLHS, APInt &DemandedRHS) { |
| 135 |
if (isa(Shuf->getType())) { |
135 |
if (isa(Shuf->getType())) { |
| 136 |
assert(DemandedElts == APInt(1,1)); |
136 |
assert(DemandedElts == APInt(1,1)); |
| 137 |
DemandedLHS = DemandedRHS = DemandedElts; |
137 |
DemandedLHS = DemandedRHS = DemandedElts; |
| 138 |
return true; |
138 |
return true; |
| 139 |
} |
139 |
} |
| 140 |
|
140 |
|
| 141 |
int NumElts = |
141 |
int NumElts = |
| 142 |
cast(Shuf->getOperand(0)->getType())->getNumElements(); |
142 |
cast(Shuf->getOperand(0)->getType())->getNumElements(); |
| 143 |
return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(), |
143 |
return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(), |
| 144 |
DemandedElts, DemandedLHS, DemandedRHS); |
144 |
DemandedElts, DemandedLHS, DemandedRHS); |
| 145 |
} |
145 |
} |
| 146 |
|
146 |
|
| 147 |
static void computeKnownBits(const Value *V, const APInt &DemandedElts, |
147 |
static void computeKnownBits(const Value *V, const APInt &DemandedElts, |
| 148 |
KnownBits &Known, unsigned Depth, |
148 |
KnownBits &Known, unsigned Depth, |
| 149 |
const SimplifyQuery &Q); |
149 |
const SimplifyQuery &Q); |
| 150 |
|
150 |
|
| 151 |
static void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, |
151 |
static void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, |
| 152 |
const SimplifyQuery &Q) { |
152 |
const SimplifyQuery &Q) { |
| 153 |
// Since the number of lanes in a scalable vector is unknown at compile time, |
153 |
// Since the number of lanes in a scalable vector is unknown at compile time, |
| 154 |
// we track one bit which is implicitly broadcast to all lanes. This means |
154 |
// we track one bit which is implicitly broadcast to all lanes. This means |
| 155 |
// that all lanes in a scalable vector are considered demanded. |
155 |
// that all lanes in a scalable vector are considered demanded. |
| 156 |
auto *FVTy = dyn_cast(V->getType()); |
156 |
auto *FVTy = dyn_cast(V->getType()); |
| 157 |
APInt DemandedElts = |
157 |
APInt DemandedElts = |
| 158 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
158 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
| 159 |
computeKnownBits(V, DemandedElts, Known, Depth, Q); |
159 |
computeKnownBits(V, DemandedElts, Known, Depth, Q); |
| 160 |
} |
160 |
} |
| 161 |
|
161 |
|
| 162 |
void llvm::computeKnownBits(const Value *V, KnownBits &Known, |
162 |
void llvm::computeKnownBits(const Value *V, KnownBits &Known, |
| 163 |
const DataLayout &DL, unsigned Depth, |
163 |
const DataLayout &DL, unsigned Depth, |
| 164 |
AssumptionCache *AC, const Instruction *CxtI, |
164 |
AssumptionCache *AC, const Instruction *CxtI, |
| 165 |
const DominatorTree *DT, bool UseInstrInfo) { |
165 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 166 |
::computeKnownBits(V, Known, Depth, |
166 |
::computeKnownBits(V, Known, Depth, |
| 167 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
167 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 168 |
safeCxtI(V, CxtI), UseInstrInfo)); |
168 |
safeCxtI(V, CxtI), UseInstrInfo)); |
| 169 |
} |
169 |
} |
| 170 |
|
170 |
|
| 171 |
void llvm::computeKnownBits(const Value *V, const APInt &DemandedElts, |
171 |
void llvm::computeKnownBits(const Value *V, const APInt &DemandedElts, |
| 172 |
KnownBits &Known, const DataLayout &DL, |
172 |
KnownBits &Known, const DataLayout &DL, |
| 173 |
unsigned Depth, AssumptionCache *AC, |
173 |
unsigned Depth, AssumptionCache *AC, |
| 174 |
const Instruction *CxtI, const DominatorTree *DT, |
174 |
const Instruction *CxtI, const DominatorTree *DT, |
| 175 |
bool UseInstrInfo) { |
175 |
bool UseInstrInfo) { |
| 176 |
::computeKnownBits(V, DemandedElts, Known, Depth, |
176 |
::computeKnownBits(V, DemandedElts, Known, Depth, |
| 177 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
177 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 178 |
safeCxtI(V, CxtI), UseInstrInfo)); |
178 |
safeCxtI(V, CxtI), UseInstrInfo)); |
| 179 |
} |
179 |
} |
| 180 |
|
180 |
|
| 181 |
static KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, |
181 |
static KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, |
| 182 |
unsigned Depth, const SimplifyQuery &Q); |
182 |
unsigned Depth, const SimplifyQuery &Q); |
| 183 |
|
183 |
|
| 184 |
static KnownBits computeKnownBits(const Value *V, unsigned Depth, |
184 |
static KnownBits computeKnownBits(const Value *V, unsigned Depth, |
| 185 |
const SimplifyQuery &Q); |
185 |
const SimplifyQuery &Q); |
| 186 |
|
186 |
|
| 187 |
KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL, |
187 |
KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL, |
| 188 |
unsigned Depth, AssumptionCache *AC, |
188 |
unsigned Depth, AssumptionCache *AC, |
| 189 |
const Instruction *CxtI, |
189 |
const Instruction *CxtI, |
| 190 |
const DominatorTree *DT, bool UseInstrInfo) { |
190 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 191 |
return ::computeKnownBits(V, Depth, |
191 |
return ::computeKnownBits(V, Depth, |
| 192 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
192 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 193 |
safeCxtI(V, CxtI), UseInstrInfo)); |
193 |
safeCxtI(V, CxtI), UseInstrInfo)); |
| 194 |
} |
194 |
} |
| 195 |
|
195 |
|
| 196 |
KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts, |
196 |
KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts, |
| 197 |
const DataLayout &DL, unsigned Depth, |
197 |
const DataLayout &DL, unsigned Depth, |
| 198 |
AssumptionCache *AC, const Instruction *CxtI, |
198 |
AssumptionCache *AC, const Instruction *CxtI, |
| 199 |
const DominatorTree *DT, bool UseInstrInfo) { |
199 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 200 |
return ::computeKnownBits(V, DemandedElts, Depth, |
200 |
return ::computeKnownBits(V, DemandedElts, Depth, |
| 201 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
201 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 202 |
safeCxtI(V, CxtI), UseInstrInfo)); |
202 |
safeCxtI(V, CxtI), UseInstrInfo)); |
| 203 |
} |
203 |
} |
| 204 |
|
204 |
|
| 205 |
bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS, |
205 |
bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS, |
| 206 |
const DataLayout &DL, AssumptionCache *AC, |
206 |
const DataLayout &DL, AssumptionCache *AC, |
| 207 |
const Instruction *CxtI, const DominatorTree *DT, |
207 |
const Instruction *CxtI, const DominatorTree *DT, |
| 208 |
bool UseInstrInfo) { |
208 |
bool UseInstrInfo) { |
| 209 |
assert(LHS->getType() == RHS->getType() && |
209 |
assert(LHS->getType() == RHS->getType() && |
| 210 |
"LHS and RHS should have the same type"); |
210 |
"LHS and RHS should have the same type"); |
| 211 |
assert(LHS->getType()->isIntOrIntVectorTy() && |
211 |
assert(LHS->getType()->isIntOrIntVectorTy() && |
| 212 |
"LHS and RHS should be integers"); |
212 |
"LHS and RHS should be integers"); |
| 213 |
// Look for an inverted mask: (X & ~M) op (Y & M). |
213 |
// Look for an inverted mask: (X & ~M) op (Y & M). |
| 214 |
{ |
214 |
{ |
| 215 |
Value *M; |
215 |
Value *M; |
| 216 |
if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) && |
216 |
if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) && |
| 217 |
match(RHS, m_c_And(m_Specific(M), m_Value()))) |
217 |
match(RHS, m_c_And(m_Specific(M), m_Value()))) |
| 218 |
return true; |
218 |
return true; |
| 219 |
if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) && |
219 |
if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) && |
| 220 |
match(LHS, m_c_And(m_Specific(M), m_Value()))) |
220 |
match(LHS, m_c_And(m_Specific(M), m_Value()))) |
| 221 |
return true; |
221 |
return true; |
| 222 |
} |
222 |
} |
| 223 |
|
223 |
|
| 224 |
// X op (Y & ~X) |
224 |
// X op (Y & ~X) |
| 225 |
if (match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())) || |
225 |
if (match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())) || |
| 226 |
match(LHS, m_c_And(m_Not(m_Specific(RHS)), m_Value()))) |
226 |
match(LHS, m_c_And(m_Not(m_Specific(RHS)), m_Value()))) |
| 227 |
return true; |
227 |
return true; |
| 228 |
|
228 |
|
| 229 |
// X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern |
229 |
// X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern |
| 230 |
// for constant Y. |
230 |
// for constant Y. |
| 231 |
Value *Y; |
231 |
Value *Y; |
| 232 |
if (match(RHS, |
232 |
if (match(RHS, |
| 233 |
m_c_Xor(m_c_And(m_Specific(LHS), m_Value(Y)), m_Deferred(Y))) || |
233 |
m_c_Xor(m_c_And(m_Specific(LHS), m_Value(Y)), m_Deferred(Y))) || |
| 234 |
match(LHS, m_c_Xor(m_c_And(m_Specific(RHS), m_Value(Y)), m_Deferred(Y)))) |
234 |
match(LHS, m_c_Xor(m_c_And(m_Specific(RHS), m_Value(Y)), m_Deferred(Y)))) |
| 235 |
return true; |
235 |
return true; |
| 236 |
|
236 |
|
| 237 |
// Peek through extends to find a 'not' of the other side: |
237 |
// Peek through extends to find a 'not' of the other side: |
| 238 |
// (ext Y) op ext(~Y) |
238 |
// (ext Y) op ext(~Y) |
| 239 |
// (ext ~Y) op ext(Y) |
239 |
// (ext ~Y) op ext(Y) |
| 240 |
if ((match(LHS, m_ZExtOrSExt(m_Value(Y))) && |
240 |
if ((match(LHS, m_ZExtOrSExt(m_Value(Y))) && |
| 241 |
match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))) || |
241 |
match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))) || |
| 242 |
(match(RHS, m_ZExtOrSExt(m_Value(Y))) && |
242 |
(match(RHS, m_ZExtOrSExt(m_Value(Y))) && |
| 243 |
match(LHS, m_ZExtOrSExt(m_Not(m_Specific(Y)))))) |
243 |
match(LHS, m_ZExtOrSExt(m_Not(m_Specific(Y)))))) |
| 244 |
return true; |
244 |
return true; |
| 245 |
|
245 |
|
| 246 |
// Look for: (A & B) op ~(A | B) |
246 |
// Look for: (A & B) op ~(A | B) |
| 247 |
{ |
247 |
{ |
| 248 |
Value *A, *B; |
248 |
Value *A, *B; |
| 249 |
if (match(LHS, m_And(m_Value(A), m_Value(B))) && |
249 |
if (match(LHS, m_And(m_Value(A), m_Value(B))) && |
| 250 |
match(RHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))) |
250 |
match(RHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))) |
| 251 |
return true; |
251 |
return true; |
| 252 |
if (match(RHS, m_And(m_Value(A), m_Value(B))) && |
252 |
if (match(RHS, m_And(m_Value(A), m_Value(B))) && |
| 253 |
match(LHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))) |
253 |
match(LHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))) |
| 254 |
return true; |
254 |
return true; |
| 255 |
} |
255 |
} |
| 256 |
IntegerType *IT = cast(LHS->getType()->getScalarType()); |
256 |
IntegerType *IT = cast(LHS->getType()->getScalarType()); |
| 257 |
KnownBits LHSKnown(IT->getBitWidth()); |
257 |
KnownBits LHSKnown(IT->getBitWidth()); |
| 258 |
KnownBits RHSKnown(IT->getBitWidth()); |
258 |
KnownBits RHSKnown(IT->getBitWidth()); |
| 259 |
computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, UseInstrInfo); |
259 |
computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, UseInstrInfo); |
| 260 |
computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, UseInstrInfo); |
260 |
computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, UseInstrInfo); |
| 261 |
return KnownBits::haveNoCommonBitsSet(LHSKnown, RHSKnown); |
261 |
return KnownBits::haveNoCommonBitsSet(LHSKnown, RHSKnown); |
| 262 |
} |
262 |
} |
| 263 |
|
263 |
|
| 264 |
bool llvm::isOnlyUsedInZeroComparison(const Instruction *I) { |
264 |
bool llvm::isOnlyUsedInZeroComparison(const Instruction *I) { |
| 265 |
return !I->user_empty() && all_of(I->users(), [](const User *U) { |
265 |
return !I->user_empty() && all_of(I->users(), [](const User *U) { |
| 266 |
ICmpInst::Predicate P; |
266 |
ICmpInst::Predicate P; |
| 267 |
return match(U, m_ICmp(P, m_Value(), m_Zero())); |
267 |
return match(U, m_ICmp(P, m_Value(), m_Zero())); |
| 268 |
}); |
268 |
}); |
| 269 |
} |
269 |
} |
| 270 |
|
270 |
|
| 271 |
bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *I) { |
271 |
bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *I) { |
| 272 |
return !I->user_empty() && all_of(I->users(), [](const User *U) { |
272 |
return !I->user_empty() && all_of(I->users(), [](const User *U) { |
| 273 |
ICmpInst::Predicate P; |
273 |
ICmpInst::Predicate P; |
| 274 |
return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P); |
274 |
return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P); |
| 275 |
}); |
275 |
}); |
| 276 |
} |
276 |
} |
| 277 |
|
277 |
|
| 278 |
static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, |
278 |
static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, |
| 279 |
const SimplifyQuery &Q); |
279 |
const SimplifyQuery &Q); |
| 280 |
|
280 |
|
| 281 |
bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, |
281 |
bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, |
| 282 |
bool OrZero, unsigned Depth, |
282 |
bool OrZero, unsigned Depth, |
| 283 |
AssumptionCache *AC, const Instruction *CxtI, |
283 |
AssumptionCache *AC, const Instruction *CxtI, |
| 284 |
const DominatorTree *DT, bool UseInstrInfo) { |
284 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 285 |
return ::isKnownToBeAPowerOfTwo(V, OrZero, Depth, |
285 |
return ::isKnownToBeAPowerOfTwo(V, OrZero, Depth, |
| 286 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
286 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 287 |
safeCxtI(V, CxtI), |
287 |
safeCxtI(V, CxtI), |
| 288 |
UseInstrInfo)); |
288 |
UseInstrInfo)); |
| 289 |
} |
289 |
} |
| 290 |
|
290 |
|
| 291 |
static bool isKnownNonZero(const Value *V, const APInt &DemandedElts, |
291 |
static bool isKnownNonZero(const Value *V, const APInt &DemandedElts, |
| 292 |
unsigned Depth, const SimplifyQuery &Q); |
292 |
unsigned Depth, const SimplifyQuery &Q); |
| 293 |
|
293 |
|
| 294 |
static bool isKnownNonZero(const Value *V, unsigned Depth, |
294 |
static bool isKnownNonZero(const Value *V, unsigned Depth, |
| 295 |
const SimplifyQuery &Q); |
295 |
const SimplifyQuery &Q); |
| 296 |
|
296 |
|
| 297 |
bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth, |
297 |
bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth, |
| 298 |
AssumptionCache *AC, const Instruction *CxtI, |
298 |
AssumptionCache *AC, const Instruction *CxtI, |
| 299 |
const DominatorTree *DT, bool UseInstrInfo) { |
299 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 300 |
return ::isKnownNonZero(V, Depth, |
300 |
return ::isKnownNonZero(V, Depth, |
| 301 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
301 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 302 |
safeCxtI(V, CxtI), UseInstrInfo)); |
302 |
safeCxtI(V, CxtI), UseInstrInfo)); |
| 303 |
} |
303 |
} |
| 304 |
|
304 |
|
| 305 |
bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL, |
305 |
bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL, |
| 306 |
unsigned Depth, AssumptionCache *AC, |
306 |
unsigned Depth, AssumptionCache *AC, |
| 307 |
const Instruction *CxtI, const DominatorTree *DT, |
307 |
const Instruction *CxtI, const DominatorTree *DT, |
| 308 |
bool UseInstrInfo) { |
308 |
bool UseInstrInfo) { |
| 309 |
KnownBits Known = computeKnownBits(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
309 |
KnownBits Known = computeKnownBits(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
| 310 |
return Known.isNonNegative(); |
310 |
return Known.isNonNegative(); |
| 311 |
} |
311 |
} |
| 312 |
|
312 |
|
| 313 |
bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth, |
313 |
bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth, |
| 314 |
AssumptionCache *AC, const Instruction *CxtI, |
314 |
AssumptionCache *AC, const Instruction *CxtI, |
| 315 |
const DominatorTree *DT, bool UseInstrInfo) { |
315 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 316 |
if (auto *CI = dyn_cast(V)) |
316 |
if (auto *CI = dyn_cast(V)) |
| 317 |
return CI->getValue().isStrictlyPositive(); |
317 |
return CI->getValue().isStrictlyPositive(); |
| 318 |
|
318 |
|
| 319 |
// TODO: We'd doing two recursive queries here. We should factor this such |
319 |
// TODO: We'd doing two recursive queries here. We should factor this such |
| 320 |
// that only a single query is needed. |
320 |
// that only a single query is needed. |
| 321 |
return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) && |
321 |
return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) && |
| 322 |
isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
322 |
isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
| 323 |
} |
323 |
} |
| 324 |
|
324 |
|
| 325 |
bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth, |
325 |
bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth, |
| 326 |
AssumptionCache *AC, const Instruction *CxtI, |
326 |
AssumptionCache *AC, const Instruction *CxtI, |
| 327 |
const DominatorTree *DT, bool UseInstrInfo) { |
327 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 328 |
KnownBits Known = computeKnownBits(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
328 |
KnownBits Known = computeKnownBits(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
| 329 |
return Known.isNegative(); |
329 |
return Known.isNegative(); |
| 330 |
} |
330 |
} |
| 331 |
|
331 |
|
| 332 |
static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth, |
332 |
static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth, |
| 333 |
const SimplifyQuery &Q); |
333 |
const SimplifyQuery &Q); |
| 334 |
|
334 |
|
| 335 |
bool llvm::isKnownNonEqual(const Value *V1, const Value *V2, |
335 |
bool llvm::isKnownNonEqual(const Value *V1, const Value *V2, |
| 336 |
const DataLayout &DL, AssumptionCache *AC, |
336 |
const DataLayout &DL, AssumptionCache *AC, |
| 337 |
const Instruction *CxtI, const DominatorTree *DT, |
337 |
const Instruction *CxtI, const DominatorTree *DT, |
| 338 |
bool UseInstrInfo) { |
338 |
bool UseInstrInfo) { |
| 339 |
return ::isKnownNonEqual(V1, V2, 0, |
339 |
return ::isKnownNonEqual(V1, V2, 0, |
| 340 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
340 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 341 |
safeCxtI(V2, V1, CxtI), UseInstrInfo)); |
341 |
safeCxtI(V2, V1, CxtI), UseInstrInfo)); |
| 342 |
} |
342 |
} |
| 343 |
|
343 |
|
| 344 |
static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, |
344 |
static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, |
| 345 |
const SimplifyQuery &Q); |
345 |
const SimplifyQuery &Q); |
| 346 |
|
346 |
|
| 347 |
bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask, |
347 |
bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask, |
| 348 |
const DataLayout &DL, unsigned Depth, |
348 |
const DataLayout &DL, unsigned Depth, |
| 349 |
AssumptionCache *AC, const Instruction *CxtI, |
349 |
AssumptionCache *AC, const Instruction *CxtI, |
| 350 |
const DominatorTree *DT, bool UseInstrInfo) { |
350 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 351 |
return ::MaskedValueIsZero(V, Mask, Depth, |
351 |
return ::MaskedValueIsZero(V, Mask, Depth, |
| 352 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
352 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 353 |
safeCxtI(V, CxtI), UseInstrInfo)); |
353 |
safeCxtI(V, CxtI), UseInstrInfo)); |
| 354 |
} |
354 |
} |
| 355 |
|
355 |
|
| 356 |
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, |
356 |
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, |
| 357 |
unsigned Depth, const SimplifyQuery &Q); |
357 |
unsigned Depth, const SimplifyQuery &Q); |
| 358 |
|
358 |
|
| 359 |
static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, |
359 |
static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, |
| 360 |
const SimplifyQuery &Q) { |
360 |
const SimplifyQuery &Q) { |
| 361 |
auto *FVTy = dyn_cast(V->getType()); |
361 |
auto *FVTy = dyn_cast(V->getType()); |
| 362 |
APInt DemandedElts = |
362 |
APInt DemandedElts = |
| 363 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
363 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
| 364 |
return ComputeNumSignBits(V, DemandedElts, Depth, Q); |
364 |
return ComputeNumSignBits(V, DemandedElts, Depth, Q); |
| 365 |
} |
365 |
} |
| 366 |
|
366 |
|
| 367 |
unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL, |
367 |
unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL, |
| 368 |
unsigned Depth, AssumptionCache *AC, |
368 |
unsigned Depth, AssumptionCache *AC, |
| 369 |
const Instruction *CxtI, |
369 |
const Instruction *CxtI, |
| 370 |
const DominatorTree *DT, bool UseInstrInfo) { |
370 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 371 |
return ::ComputeNumSignBits(V, Depth, |
371 |
return ::ComputeNumSignBits(V, Depth, |
| 372 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
372 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 373 |
safeCxtI(V, CxtI), UseInstrInfo)); |
373 |
safeCxtI(V, CxtI), UseInstrInfo)); |
| 374 |
} |
374 |
} |
| 375 |
|
375 |
|
| 376 |
unsigned llvm::ComputeMaxSignificantBits(const Value *V, const DataLayout &DL, |
376 |
unsigned llvm::ComputeMaxSignificantBits(const Value *V, const DataLayout &DL, |
| 377 |
unsigned Depth, AssumptionCache *AC, |
377 |
unsigned Depth, AssumptionCache *AC, |
| 378 |
const Instruction *CxtI, |
378 |
const Instruction *CxtI, |
| 379 |
const DominatorTree *DT) { |
379 |
const DominatorTree *DT) { |
| 380 |
unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT); |
380 |
unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT); |
| 381 |
return V->getType()->getScalarSizeInBits() - SignBits + 1; |
381 |
return V->getType()->getScalarSizeInBits() - SignBits + 1; |
| 382 |
} |
382 |
} |
| 383 |
|
383 |
|
| 384 |
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, |
384 |
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, |
| 385 |
bool NSW, const APInt &DemandedElts, |
385 |
bool NSW, const APInt &DemandedElts, |
| 386 |
KnownBits &KnownOut, KnownBits &Known2, |
386 |
KnownBits &KnownOut, KnownBits &Known2, |
| 387 |
unsigned Depth, const SimplifyQuery &Q) { |
387 |
unsigned Depth, const SimplifyQuery &Q) { |
| 388 |
computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q); |
388 |
computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q); |
| 389 |
|
389 |
|
| 390 |
// If one operand is unknown and we have no nowrap information, |
390 |
// If one operand is unknown and we have no nowrap information, |
| 391 |
// the result will be unknown independently of the second operand. |
391 |
// the result will be unknown independently of the second operand. |
| 392 |
if (KnownOut.isUnknown() && !NSW) |
392 |
if (KnownOut.isUnknown() && !NSW) |
| 393 |
return; |
393 |
return; |
| 394 |
|
394 |
|
| 395 |
computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q); |
395 |
computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q); |
| 396 |
KnownOut = KnownBits::computeForAddSub(Add, NSW, Known2, KnownOut); |
396 |
KnownOut = KnownBits::computeForAddSub(Add, NSW, Known2, KnownOut); |
| 397 |
} |
397 |
} |
| 398 |
|
398 |
|
| 399 |
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, |
399 |
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, |
| 400 |
const APInt &DemandedElts, KnownBits &Known, |
400 |
const APInt &DemandedElts, KnownBits &Known, |
| 401 |
KnownBits &Known2, unsigned Depth, |
401 |
KnownBits &Known2, unsigned Depth, |
| 402 |
const SimplifyQuery &Q) { |
402 |
const SimplifyQuery &Q) { |
| 403 |
computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q); |
403 |
computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q); |
| 404 |
computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q); |
404 |
computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q); |
| 405 |
|
405 |
|
| 406 |
bool isKnownNegative = false; |
406 |
bool isKnownNegative = false; |
| 407 |
bool isKnownNonNegative = false; |
407 |
bool isKnownNonNegative = false; |
| 408 |
// If the multiplication is known not to overflow, compute the sign bit. |
408 |
// If the multiplication is known not to overflow, compute the sign bit. |
| 409 |
if (NSW) { |
409 |
if (NSW) { |
| 410 |
if (Op0 == Op1) { |
410 |
if (Op0 == Op1) { |
| 411 |
// The product of a number with itself is non-negative. |
411 |
// The product of a number with itself is non-negative. |
| 412 |
isKnownNonNegative = true; |
412 |
isKnownNonNegative = true; |
| 413 |
} else { |
413 |
} else { |
| 414 |
bool isKnownNonNegativeOp1 = Known.isNonNegative(); |
414 |
bool isKnownNonNegativeOp1 = Known.isNonNegative(); |
| 415 |
bool isKnownNonNegativeOp0 = Known2.isNonNegative(); |
415 |
bool isKnownNonNegativeOp0 = Known2.isNonNegative(); |
| 416 |
bool isKnownNegativeOp1 = Known.isNegative(); |
416 |
bool isKnownNegativeOp1 = Known.isNegative(); |
| 417 |
bool isKnownNegativeOp0 = Known2.isNegative(); |
417 |
bool isKnownNegativeOp0 = Known2.isNegative(); |
| 418 |
// The product of two numbers with the same sign is non-negative. |
418 |
// The product of two numbers with the same sign is non-negative. |
| 419 |
isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) || |
419 |
isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) || |
| 420 |
(isKnownNonNegativeOp1 && isKnownNonNegativeOp0); |
420 |
(isKnownNonNegativeOp1 && isKnownNonNegativeOp0); |
| 421 |
// The product of a negative number and a non-negative number is either |
421 |
// The product of a negative number and a non-negative number is either |
| 422 |
// negative or zero. |
422 |
// negative or zero. |
| 423 |
if (!isKnownNonNegative) |
423 |
if (!isKnownNonNegative) |
| 424 |
isKnownNegative = |
424 |
isKnownNegative = |
| 425 |
(isKnownNegativeOp1 && isKnownNonNegativeOp0 && |
425 |
(isKnownNegativeOp1 && isKnownNonNegativeOp0 && |
| 426 |
Known2.isNonZero()) || |
426 |
Known2.isNonZero()) || |
| 427 |
(isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero()); |
427 |
(isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero()); |
| 428 |
} |
428 |
} |
| 429 |
} |
429 |
} |
| 430 |
|
430 |
|
| 431 |
bool SelfMultiply = Op0 == Op1; |
431 |
bool SelfMultiply = Op0 == Op1; |
| 432 |
// TODO: SelfMultiply can be poison, but not undef. |
432 |
// TODO: SelfMultiply can be poison, but not undef. |
| 433 |
if (SelfMultiply) |
433 |
if (SelfMultiply) |
| 434 |
SelfMultiply &= |
434 |
SelfMultiply &= |
| 435 |
isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1); |
435 |
isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1); |
| 436 |
Known = KnownBits::mul(Known, Known2, SelfMultiply); |
436 |
Known = KnownBits::mul(Known, Known2, SelfMultiply); |
| 437 |
|
437 |
|
| 438 |
// Only make use of no-wrap flags if we failed to compute the sign bit |
438 |
// Only make use of no-wrap flags if we failed to compute the sign bit |
| 439 |
// directly. This matters if the multiplication always overflows, in |
439 |
// directly. This matters if the multiplication always overflows, in |
| 440 |
// which case we prefer to follow the result of the direct computation, |
440 |
// which case we prefer to follow the result of the direct computation, |
| 441 |
// though as the program is invoking undefined behaviour we can choose |
441 |
// though as the program is invoking undefined behaviour we can choose |
| 442 |
// whatever we like here. |
442 |
// whatever we like here. |
| 443 |
if (isKnownNonNegative && !Known.isNegative()) |
443 |
if (isKnownNonNegative && !Known.isNegative()) |
| 444 |
Known.makeNonNegative(); |
444 |
Known.makeNonNegative(); |
| 445 |
else if (isKnownNegative && !Known.isNonNegative()) |
445 |
else if (isKnownNegative && !Known.isNonNegative()) |
| 446 |
Known.makeNegative(); |
446 |
Known.makeNegative(); |
| 447 |
} |
447 |
} |
| 448 |
|
448 |
|
| 449 |
void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges, |
449 |
void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges, |
| 450 |
KnownBits &Known) { |
450 |
KnownBits &Known) { |
| 451 |
unsigned BitWidth = Known.getBitWidth(); |
451 |
unsigned BitWidth = Known.getBitWidth(); |
| 452 |
unsigned NumRanges = Ranges.getNumOperands() / 2; |
452 |
unsigned NumRanges = Ranges.getNumOperands() / 2; |
| 453 |
assert(NumRanges >= 1); |
453 |
assert(NumRanges >= 1); |
| 454 |
|
454 |
|
| 455 |
Known.Zero.setAllBits(); |
455 |
Known.Zero.setAllBits(); |
| 456 |
Known.One.setAllBits(); |
456 |
Known.One.setAllBits(); |
| 457 |
|
457 |
|
| 458 |
for (unsigned i = 0; i < NumRanges; ++i) { |
458 |
for (unsigned i = 0; i < NumRanges; ++i) { |
| 459 |
ConstantInt *Lower = |
459 |
ConstantInt *Lower = |
| 460 |
mdconst::extract(Ranges.getOperand(2 * i + 0)); |
460 |
mdconst::extract(Ranges.getOperand(2 * i + 0)); |
| 461 |
ConstantInt *Upper = |
461 |
ConstantInt *Upper = |
| 462 |
mdconst::extract(Ranges.getOperand(2 * i + 1)); |
462 |
mdconst::extract(Ranges.getOperand(2 * i + 1)); |
| 463 |
ConstantRange Range(Lower->getValue(), Upper->getValue()); |
463 |
ConstantRange Range(Lower->getValue(), Upper->getValue()); |
| 464 |
|
464 |
|
| 465 |
// The first CommonPrefixBits of all values in Range are equal. |
465 |
// The first CommonPrefixBits of all values in Range are equal. |
| 466 |
unsigned CommonPrefixBits = |
466 |
unsigned CommonPrefixBits = |
| 467 |
(Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero(); |
467 |
(Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero(); |
| 468 |
APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits); |
468 |
APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits); |
| 469 |
APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth); |
469 |
APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth); |
| 470 |
Known.One &= UnsignedMax & Mask; |
470 |
Known.One &= UnsignedMax & Mask; |
| 471 |
Known.Zero &= ~UnsignedMax & Mask; |
471 |
Known.Zero &= ~UnsignedMax & Mask; |
| 472 |
} |
472 |
} |
| 473 |
} |
473 |
} |
| 474 |
|
474 |
|
| 475 |
static bool isEphemeralValueOf(const Instruction *I, const Value *E) { |
475 |
static bool isEphemeralValueOf(const Instruction *I, const Value *E) { |
| 476 |
SmallVector WorkSet(1, I); |
476 |
SmallVector WorkSet(1, I); |
| 477 |
SmallPtrSet Visited; |
477 |
SmallPtrSet Visited; |
| 478 |
SmallPtrSet EphValues; |
478 |
SmallPtrSet EphValues; |
| 479 |
|
479 |
|
| 480 |
// The instruction defining an assumption's condition itself is always |
480 |
// The instruction defining an assumption's condition itself is always |
| 481 |
// considered ephemeral to that assumption (even if it has other |
481 |
// considered ephemeral to that assumption (even if it has other |
| 482 |
// non-ephemeral users). See r246696's test case for an example. |
482 |
// non-ephemeral users). See r246696's test case for an example. |
| 483 |
if (is_contained(I->operands(), E)) |
483 |
if (is_contained(I->operands(), E)) |
| 484 |
return true; |
484 |
return true; |
| 485 |
|
485 |
|
| 486 |
while (!WorkSet.empty()) { |
486 |
while (!WorkSet.empty()) { |
| 487 |
const Value *V = WorkSet.pop_back_val(); |
487 |
const Value *V = WorkSet.pop_back_val(); |
| 488 |
if (!Visited.insert(V).second) |
488 |
if (!Visited.insert(V).second) |
| 489 |
continue; |
489 |
continue; |
| 490 |
|
490 |
|
| 491 |
// If all uses of this value are ephemeral, then so is this value. |
491 |
// If all uses of this value are ephemeral, then so is this value. |
| 492 |
if (llvm::all_of(V->users(), [&](const User *U) { |
492 |
if (llvm::all_of(V->users(), [&](const User *U) { |
| 493 |
return EphValues.count(U); |
493 |
return EphValues.count(U); |
| 494 |
})) { |
494 |
})) { |
| 495 |
if (V == E) |
495 |
if (V == E) |
| 496 |
return true; |
496 |
return true; |
| 497 |
|
497 |
|
| 498 |
if (V == I || (isa(V) && |
498 |
if (V == I || (isa(V) && |
| 499 |
!cast(V)->mayHaveSideEffects() && |
499 |
!cast(V)->mayHaveSideEffects() && |
| 500 |
!cast(V)->isTerminator())) { |
500 |
!cast(V)->isTerminator())) { |
| 501 |
EphValues.insert(V); |
501 |
EphValues.insert(V); |
| 502 |
if (const User *U = dyn_cast(V)) |
502 |
if (const User *U = dyn_cast(V)) |
| 503 |
append_range(WorkSet, U->operands()); |
503 |
append_range(WorkSet, U->operands()); |
| 504 |
} |
504 |
} |
| 505 |
} |
505 |
} |
| 506 |
} |
506 |
} |
| 507 |
|
507 |
|
| 508 |
return false; |
508 |
return false; |
| 509 |
} |
509 |
} |
| 510 |
|
510 |
|
| 511 |
// Is this an intrinsic that cannot be speculated but also cannot trap? |
511 |
// Is this an intrinsic that cannot be speculated but also cannot trap? |
| 512 |
bool llvm::isAssumeLikeIntrinsic(const Instruction *I) { |
512 |
bool llvm::isAssumeLikeIntrinsic(const Instruction *I) { |
| 513 |
if (const IntrinsicInst *CI = dyn_cast(I)) |
513 |
if (const IntrinsicInst *CI = dyn_cast(I)) |
| 514 |
return CI->isAssumeLikeIntrinsic(); |
514 |
return CI->isAssumeLikeIntrinsic(); |
| 515 |
|
515 |
|
| 516 |
return false; |
516 |
return false; |
| 517 |
} |
517 |
} |
| 518 |
|
518 |
|
| 519 |
bool llvm::isValidAssumeForContext(const Instruction *Inv, |
519 |
bool llvm::isValidAssumeForContext(const Instruction *Inv, |
| 520 |
const Instruction *CxtI, |
520 |
const Instruction *CxtI, |
| 521 |
const DominatorTree *DT) { |
521 |
const DominatorTree *DT) { |
| 522 |
// There are two restrictions on the use of an assume: |
522 |
// There are two restrictions on the use of an assume: |
| 523 |
// 1. The assume must dominate the context (or the control flow must |
523 |
// 1. The assume must dominate the context (or the control flow must |
| 524 |
// reach the assume whenever it reaches the context). |
524 |
// reach the assume whenever it reaches the context). |
| 525 |
// 2. The context must not be in the assume's set of ephemeral values |
525 |
// 2. The context must not be in the assume's set of ephemeral values |
| 526 |
// (otherwise we will use the assume to prove that the condition |
526 |
// (otherwise we will use the assume to prove that the condition |
| 527 |
// feeding the assume is trivially true, thus causing the removal of |
527 |
// feeding the assume is trivially true, thus causing the removal of |
| 528 |
// the assume). |
528 |
// the assume). |
| 529 |
|
529 |
|
| 530 |
if (Inv->getParent() == CxtI->getParent()) { |
530 |
if (Inv->getParent() == CxtI->getParent()) { |
| 531 |
// If Inv and CtxI are in the same block, check if the assume (Inv) is first |
531 |
// If Inv and CtxI are in the same block, check if the assume (Inv) is first |
| 532 |
// in the BB. |
532 |
// in the BB. |
| 533 |
if (Inv->comesBefore(CxtI)) |
533 |
if (Inv->comesBefore(CxtI)) |
| 534 |
return true; |
534 |
return true; |
| 535 |
|
535 |
|
| 536 |
// Don't let an assume affect itself - this would cause the problems |
536 |
// Don't let an assume affect itself - this would cause the problems |
| 537 |
// `isEphemeralValueOf` is trying to prevent, and it would also make |
537 |
// `isEphemeralValueOf` is trying to prevent, and it would also make |
| 538 |
// the loop below go out of bounds. |
538 |
// the loop below go out of bounds. |
| 539 |
if (Inv == CxtI) |
539 |
if (Inv == CxtI) |
| 540 |
return false; |
540 |
return false; |
| 541 |
|
541 |
|
| 542 |
// The context comes first, but they're both in the same block. |
542 |
// The context comes first, but they're both in the same block. |
| 543 |
// Make sure there is nothing in between that might interrupt |
543 |
// Make sure there is nothing in between that might interrupt |
| 544 |
// the control flow, not even CxtI itself. |
544 |
// the control flow, not even CxtI itself. |
| 545 |
// We limit the scan distance between the assume and its context instruction |
545 |
// We limit the scan distance between the assume and its context instruction |
| 546 |
// to avoid a compile-time explosion. This limit is chosen arbitrarily, so |
546 |
// to avoid a compile-time explosion. This limit is chosen arbitrarily, so |
| 547 |
// it can be adjusted if needed (could be turned into a cl::opt). |
547 |
// it can be adjusted if needed (could be turned into a cl::opt). |
| 548 |
auto Range = make_range(CxtI->getIterator(), Inv->getIterator()); |
548 |
auto Range = make_range(CxtI->getIterator(), Inv->getIterator()); |
| 549 |
if (!isGuaranteedToTransferExecutionToSuccessor(Range, 15)) |
549 |
if (!isGuaranteedToTransferExecutionToSuccessor(Range, 15)) |
| 550 |
return false; |
550 |
return false; |
| 551 |
|
551 |
|
| 552 |
return !isEphemeralValueOf(Inv, CxtI); |
552 |
return !isEphemeralValueOf(Inv, CxtI); |
| 553 |
} |
553 |
} |
| 554 |
|
554 |
|
| 555 |
// Inv and CxtI are in different blocks. |
555 |
// Inv and CxtI are in different blocks. |
| 556 |
if (DT) { |
556 |
if (DT) { |
| 557 |
if (DT->dominates(Inv, CxtI)) |
557 |
if (DT->dominates(Inv, CxtI)) |
| 558 |
return true; |
558 |
return true; |
| 559 |
} else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) { |
559 |
} else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) { |
| 560 |
// We don't have a DT, but this trivially dominates. |
560 |
// We don't have a DT, but this trivially dominates. |
| 561 |
return true; |
561 |
return true; |
| 562 |
} |
562 |
} |
| 563 |
|
563 |
|
| 564 |
return false; |
564 |
return false; |
| 565 |
} |
565 |
} |
| 566 |
|
566 |
|
| 567 |
// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but |
567 |
// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but |
| 568 |
// we still have enough information about `RHS` to conclude non-zero. For |
568 |
// we still have enough information about `RHS` to conclude non-zero. For |
| 569 |
// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops |
569 |
// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops |
| 570 |
// so the extra compile time may not be worth it, but possibly a second API |
570 |
// so the extra compile time may not be worth it, but possibly a second API |
| 571 |
// should be created for use outside of loops. |
571 |
// should be created for use outside of loops. |
| 572 |
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) { |
572 |
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) { |
| 573 |
// v u> y implies v != 0. |
573 |
// v u> y implies v != 0. |
| 574 |
if (Pred == ICmpInst::ICMP_UGT) |
574 |
if (Pred == ICmpInst::ICMP_UGT) |
| 575 |
return true; |
575 |
return true; |
| 576 |
|
576 |
|
| 577 |
// Special-case v != 0 to also handle v != null. |
577 |
// Special-case v != 0 to also handle v != null. |
| 578 |
if (Pred == ICmpInst::ICMP_NE) |
578 |
if (Pred == ICmpInst::ICMP_NE) |
| 579 |
return match(RHS, m_Zero()); |
579 |
return match(RHS, m_Zero()); |
| 580 |
|
580 |
|
| 581 |
// All other predicates - rely on generic ConstantRange handling. |
581 |
// All other predicates - rely on generic ConstantRange handling. |
| 582 |
const APInt *C; |
582 |
const APInt *C; |
| 583 |
if (!match(RHS, m_APInt(C))) |
583 |
if (!match(RHS, m_APInt(C))) |
| 584 |
return false; |
584 |
return false; |
| 585 |
|
585 |
|
| 586 |
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C); |
586 |
ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C); |
| 587 |
return !TrueValues.contains(APInt::getZero(C->getBitWidth())); |
587 |
return !TrueValues.contains(APInt::getZero(C->getBitWidth())); |
| 588 |
} |
588 |
} |
| 589 |
|
589 |
|
| 590 |
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) { |
590 |
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) { |
| 591 |
// Use of assumptions is context-sensitive. If we don't have a context, we |
591 |
// Use of assumptions is context-sensitive. If we don't have a context, we |
| 592 |
// cannot use them! |
592 |
// cannot use them! |
| 593 |
if (!Q.AC || !Q.CxtI) |
593 |
if (!Q.AC || !Q.CxtI) |
| 594 |
return false; |
594 |
return false; |
| 595 |
|
595 |
|
| 596 |
if (Q.CxtI && V->getType()->isPointerTy()) { |
596 |
if (Q.CxtI && V->getType()->isPointerTy()) { |
| 597 |
SmallVector AttrKinds{Attribute::NonNull}; |
597 |
SmallVector AttrKinds{Attribute::NonNull}; |
| 598 |
if (!NullPointerIsDefined(Q.CxtI->getFunction(), |
598 |
if (!NullPointerIsDefined(Q.CxtI->getFunction(), |
| 599 |
V->getType()->getPointerAddressSpace())) |
599 |
V->getType()->getPointerAddressSpace())) |
| 600 |
AttrKinds.push_back(Attribute::Dereferenceable); |
600 |
AttrKinds.push_back(Attribute::Dereferenceable); |
| 601 |
|
601 |
|
| 602 |
if (getKnowledgeValidInContext(V, AttrKinds, Q.CxtI, Q.DT, Q.AC)) |
602 |
if (getKnowledgeValidInContext(V, AttrKinds, Q.CxtI, Q.DT, Q.AC)) |
| 603 |
return true; |
603 |
return true; |
| 604 |
} |
604 |
} |
| 605 |
|
605 |
|
| 606 |
for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { |
606 |
for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { |
| 607 |
if (!AssumeVH) |
607 |
if (!AssumeVH) |
| 608 |
continue; |
608 |
continue; |
| 609 |
CallInst *I = cast(AssumeVH); |
609 |
CallInst *I = cast(AssumeVH); |
| 610 |
assert(I->getFunction() == Q.CxtI->getFunction() && |
610 |
assert(I->getFunction() == Q.CxtI->getFunction() && |
| 611 |
"Got assumption for the wrong function!"); |
611 |
"Got assumption for the wrong function!"); |
| 612 |
|
612 |
|
| 613 |
// Warning: This loop can end up being somewhat performance sensitive. |
613 |
// Warning: This loop can end up being somewhat performance sensitive. |
| 614 |
// We're running this loop for once for each value queried resulting in a |
614 |
// We're running this loop for once for each value queried resulting in a |
| 615 |
// runtime of ~O(#assumes * #values). |
615 |
// runtime of ~O(#assumes * #values). |
| 616 |
|
616 |
|
| 617 |
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
617 |
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
| 618 |
"must be an assume intrinsic"); |
618 |
"must be an assume intrinsic"); |
| 619 |
|
619 |
|
| 620 |
Value *RHS; |
620 |
Value *RHS; |
| 621 |
CmpInst::Predicate Pred; |
621 |
CmpInst::Predicate Pred; |
| 622 |
auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V))); |
622 |
auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V))); |
| 623 |
if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS)))) |
623 |
if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS)))) |
| 624 |
return false; |
624 |
return false; |
| 625 |
|
625 |
|
| 626 |
if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT)) |
626 |
if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT)) |
| 627 |
return true; |
627 |
return true; |
| 628 |
} |
628 |
} |
| 629 |
|
629 |
|
| 630 |
return false; |
630 |
return false; |
| 631 |
} |
631 |
} |
| 632 |
|
632 |
|
| 633 |
static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp, |
633 |
static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp, |
| 634 |
KnownBits &Known, unsigned Depth, |
634 |
KnownBits &Known, unsigned Depth, |
| 635 |
const SimplifyQuery &Q) { |
635 |
const SimplifyQuery &Q) { |
| 636 |
unsigned BitWidth = Known.getBitWidth(); |
636 |
unsigned BitWidth = Known.getBitWidth(); |
| 637 |
// We are attempting to compute known bits for the operands of an assume. |
637 |
// We are attempting to compute known bits for the operands of an assume. |
| 638 |
// Do not try to use other assumptions for those recursive calls because |
638 |
// Do not try to use other assumptions for those recursive calls because |
| 639 |
// that can lead to mutual recursion and a compile-time explosion. |
639 |
// that can lead to mutual recursion and a compile-time explosion. |
| 640 |
// An example of the mutual recursion: computeKnownBits can call |
640 |
// An example of the mutual recursion: computeKnownBits can call |
| 641 |
// isKnownNonZero which calls computeKnownBitsFromAssume (this function) |
641 |
// isKnownNonZero which calls computeKnownBitsFromAssume (this function) |
| 642 |
// and so on. |
642 |
// and so on. |
| 643 |
SimplifyQuery QueryNoAC = Q; |
643 |
SimplifyQuery QueryNoAC = Q; |
| 644 |
QueryNoAC.AC = nullptr; |
644 |
QueryNoAC.AC = nullptr; |
| 645 |
|
645 |
|
| 646 |
// Note that ptrtoint may change the bitwidth. |
646 |
// Note that ptrtoint may change the bitwidth. |
| 647 |
Value *A, *B; |
647 |
Value *A, *B; |
| 648 |
auto m_V = |
648 |
auto m_V = |
| 649 |
m_CombineOr(m_Specific(V), m_PtrToIntSameSize(Q.DL, m_Specific(V))); |
649 |
m_CombineOr(m_Specific(V), m_PtrToIntSameSize(Q.DL, m_Specific(V))); |
| 650 |
|
650 |
|
| 651 |
CmpInst::Predicate Pred; |
651 |
CmpInst::Predicate Pred; |
| 652 |
uint64_t C; |
652 |
uint64_t C; |
| 653 |
switch (Cmp->getPredicate()) { |
653 |
switch (Cmp->getPredicate()) { |
| 654 |
case ICmpInst::ICMP_EQ: |
654 |
case ICmpInst::ICMP_EQ: |
| 655 |
// assume(v = a) |
655 |
// assume(v = a) |
| 656 |
if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A)))) { |
656 |
if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A)))) { |
| 657 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
657 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 658 |
Known = Known.unionWith(RHSKnown); |
658 |
Known = Known.unionWith(RHSKnown); |
| 659 |
// assume(v & b = a) |
659 |
// assume(v & b = a) |
| 660 |
} else if (match(Cmp, |
660 |
} else if (match(Cmp, |
| 661 |
m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A)))) { |
661 |
m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A)))) { |
| 662 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
662 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 663 |
KnownBits MaskKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
663 |
KnownBits MaskKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
| 664 |
|
664 |
|
| 665 |
// For those bits in the mask that are known to be one, we can propagate |
665 |
// For those bits in the mask that are known to be one, we can propagate |
| 666 |
// known bits from the RHS to V. |
666 |
// known bits from the RHS to V. |
| 667 |
Known.Zero |= RHSKnown.Zero & MaskKnown.One; |
667 |
Known.Zero |= RHSKnown.Zero & MaskKnown.One; |
| 668 |
Known.One |= RHSKnown.One & MaskKnown.One; |
668 |
Known.One |= RHSKnown.One & MaskKnown.One; |
| 669 |
// assume(~(v & b) = a) |
669 |
// assume(~(v & b) = a) |
| 670 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))), |
670 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))), |
| 671 |
m_Value(A)))) { |
671 |
m_Value(A)))) { |
| 672 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
672 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 673 |
KnownBits MaskKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
673 |
KnownBits MaskKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
| 674 |
|
674 |
|
| 675 |
// For those bits in the mask that are known to be one, we can propagate |
675 |
// For those bits in the mask that are known to be one, we can propagate |
| 676 |
// inverted known bits from the RHS to V. |
676 |
// inverted known bits from the RHS to V. |
| 677 |
Known.Zero |= RHSKnown.One & MaskKnown.One; |
677 |
Known.Zero |= RHSKnown.One & MaskKnown.One; |
| 678 |
Known.One |= RHSKnown.Zero & MaskKnown.One; |
678 |
Known.One |= RHSKnown.Zero & MaskKnown.One; |
| 679 |
// assume(v | b = a) |
679 |
// assume(v | b = a) |
| 680 |
} else if (match(Cmp, |
680 |
} else if (match(Cmp, |
| 681 |
m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A)))) { |
681 |
m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A)))) { |
| 682 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
682 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 683 |
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
683 |
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
| 684 |
|
684 |
|
| 685 |
// For those bits in B that are known to be zero, we can propagate known |
685 |
// For those bits in B that are known to be zero, we can propagate known |
| 686 |
// bits from the RHS to V. |
686 |
// bits from the RHS to V. |
| 687 |
Known.Zero |= RHSKnown.Zero & BKnown.Zero; |
687 |
Known.Zero |= RHSKnown.Zero & BKnown.Zero; |
| 688 |
Known.One |= RHSKnown.One & BKnown.Zero; |
688 |
Known.One |= RHSKnown.One & BKnown.Zero; |
| 689 |
// assume(~(v | b) = a) |
689 |
// assume(~(v | b) = a) |
| 690 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))), |
690 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))), |
| 691 |
m_Value(A)))) { |
691 |
m_Value(A)))) { |
| 692 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
692 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 693 |
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
693 |
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
| 694 |
|
694 |
|
| 695 |
// For those bits in B that are known to be zero, we can propagate |
695 |
// For those bits in B that are known to be zero, we can propagate |
| 696 |
// inverted known bits from the RHS to V. |
696 |
// inverted known bits from the RHS to V. |
| 697 |
Known.Zero |= RHSKnown.One & BKnown.Zero; |
697 |
Known.Zero |= RHSKnown.One & BKnown.Zero; |
| 698 |
Known.One |= RHSKnown.Zero & BKnown.Zero; |
698 |
Known.One |= RHSKnown.Zero & BKnown.Zero; |
| 699 |
// assume(v ^ b = a) |
699 |
// assume(v ^ b = a) |
| 700 |
} else if (match(Cmp, |
700 |
} else if (match(Cmp, |
| 701 |
m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A)))) { |
701 |
m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A)))) { |
| 702 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
702 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 703 |
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
703 |
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
| 704 |
|
704 |
|
| 705 |
// For those bits in B that are known to be zero, we can propagate known |
705 |
// For those bits in B that are known to be zero, we can propagate known |
| 706 |
// bits from the RHS to V. For those bits in B that are known to be one, |
706 |
// bits from the RHS to V. For those bits in B that are known to be one, |
| 707 |
// we can propagate inverted known bits from the RHS to V. |
707 |
// we can propagate inverted known bits from the RHS to V. |
| 708 |
Known.Zero |= RHSKnown.Zero & BKnown.Zero; |
708 |
Known.Zero |= RHSKnown.Zero & BKnown.Zero; |
| 709 |
Known.One |= RHSKnown.One & BKnown.Zero; |
709 |
Known.One |= RHSKnown.One & BKnown.Zero; |
| 710 |
Known.Zero |= RHSKnown.One & BKnown.One; |
710 |
Known.Zero |= RHSKnown.One & BKnown.One; |
| 711 |
Known.One |= RHSKnown.Zero & BKnown.One; |
711 |
Known.One |= RHSKnown.Zero & BKnown.One; |
| 712 |
// assume(~(v ^ b) = a) |
712 |
// assume(~(v ^ b) = a) |
| 713 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))), |
713 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))), |
| 714 |
m_Value(A)))) { |
714 |
m_Value(A)))) { |
| 715 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
715 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 716 |
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
716 |
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC); |
| 717 |
|
717 |
|
| 718 |
// For those bits in B that are known to be zero, we can propagate |
718 |
// For those bits in B that are known to be zero, we can propagate |
| 719 |
// inverted known bits from the RHS to V. For those bits in B that are |
719 |
// inverted known bits from the RHS to V. For those bits in B that are |
| 720 |
// known to be one, we can propagate known bits from the RHS to V. |
720 |
// known to be one, we can propagate known bits from the RHS to V. |
| 721 |
Known.Zero |= RHSKnown.One & BKnown.Zero; |
721 |
Known.Zero |= RHSKnown.One & BKnown.Zero; |
| 722 |
Known.One |= RHSKnown.Zero & BKnown.Zero; |
722 |
Known.One |= RHSKnown.Zero & BKnown.Zero; |
| 723 |
Known.Zero |= RHSKnown.Zero & BKnown.One; |
723 |
Known.Zero |= RHSKnown.Zero & BKnown.One; |
| 724 |
Known.One |= RHSKnown.One & BKnown.One; |
724 |
Known.One |= RHSKnown.One & BKnown.One; |
| 725 |
// assume(v << c = a) |
725 |
// assume(v << c = a) |
| 726 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)), |
726 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)), |
| 727 |
m_Value(A))) && |
727 |
m_Value(A))) && |
| 728 |
C < BitWidth) { |
728 |
C < BitWidth) { |
| 729 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
729 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 730 |
|
730 |
|
| 731 |
// For those bits in RHS that are known, we can propagate them to known |
731 |
// For those bits in RHS that are known, we can propagate them to known |
| 732 |
// bits in V shifted to the right by C. |
732 |
// bits in V shifted to the right by C. |
| 733 |
RHSKnown.Zero.lshrInPlace(C); |
733 |
RHSKnown.Zero.lshrInPlace(C); |
| 734 |
RHSKnown.One.lshrInPlace(C); |
734 |
RHSKnown.One.lshrInPlace(C); |
| 735 |
Known = Known.unionWith(RHSKnown); |
735 |
Known = Known.unionWith(RHSKnown); |
| 736 |
// assume(~(v << c) = a) |
736 |
// assume(~(v << c) = a) |
| 737 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))), |
737 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))), |
| 738 |
m_Value(A))) && |
738 |
m_Value(A))) && |
| 739 |
C < BitWidth) { |
739 |
C < BitWidth) { |
| 740 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
740 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 741 |
// For those bits in RHS that are known, we can propagate them inverted |
741 |
// For those bits in RHS that are known, we can propagate them inverted |
| 742 |
// to known bits in V shifted to the right by C. |
742 |
// to known bits in V shifted to the right by C. |
| 743 |
RHSKnown.One.lshrInPlace(C); |
743 |
RHSKnown.One.lshrInPlace(C); |
| 744 |
Known.Zero |= RHSKnown.One; |
744 |
Known.Zero |= RHSKnown.One; |
| 745 |
RHSKnown.Zero.lshrInPlace(C); |
745 |
RHSKnown.Zero.lshrInPlace(C); |
| 746 |
Known.One |= RHSKnown.Zero; |
746 |
Known.One |= RHSKnown.Zero; |
| 747 |
// assume(v >> c = a) |
747 |
// assume(v >> c = a) |
| 748 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)), |
748 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)), |
| 749 |
m_Value(A))) && |
749 |
m_Value(A))) && |
| 750 |
C < BitWidth) { |
750 |
C < BitWidth) { |
| 751 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
751 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 752 |
// For those bits in RHS that are known, we can propagate them to known |
752 |
// For those bits in RHS that are known, we can propagate them to known |
| 753 |
// bits in V shifted to the right by C. |
753 |
// bits in V shifted to the right by C. |
| 754 |
Known.Zero |= RHSKnown.Zero << C; |
754 |
Known.Zero |= RHSKnown.Zero << C; |
| 755 |
Known.One |= RHSKnown.One << C; |
755 |
Known.One |= RHSKnown.One << C; |
| 756 |
// assume(~(v >> c) = a) |
756 |
// assume(~(v >> c) = a) |
| 757 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))), |
757 |
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))), |
| 758 |
m_Value(A))) && |
758 |
m_Value(A))) && |
| 759 |
C < BitWidth) { |
759 |
C < BitWidth) { |
| 760 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
760 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 761 |
// For those bits in RHS that are known, we can propagate them inverted |
761 |
// For those bits in RHS that are known, we can propagate them inverted |
| 762 |
// to known bits in V shifted to the right by C. |
762 |
// to known bits in V shifted to the right by C. |
| 763 |
Known.Zero |= RHSKnown.One << C; |
763 |
Known.Zero |= RHSKnown.One << C; |
| 764 |
Known.One |= RHSKnown.Zero << C; |
764 |
Known.One |= RHSKnown.Zero << C; |
| 765 |
} |
765 |
} |
| 766 |
break; |
766 |
break; |
| 767 |
case ICmpInst::ICMP_NE: { |
767 |
case ICmpInst::ICMP_NE: { |
| 768 |
// assume (v & b != 0) where b is a power of 2 |
768 |
// assume (v & b != 0) where b is a power of 2 |
| 769 |
const APInt *BPow2; |
769 |
const APInt *BPow2; |
| 770 |
if (match(Cmp, m_ICmp(Pred, m_c_And(m_V, m_Power2(BPow2)), m_Zero()))) { |
770 |
if (match(Cmp, m_ICmp(Pred, m_c_And(m_V, m_Power2(BPow2)), m_Zero()))) { |
| 771 |
Known.One |= *BPow2; |
771 |
Known.One |= *BPow2; |
| 772 |
} |
772 |
} |
| 773 |
break; |
773 |
break; |
| 774 |
} |
774 |
} |
| 775 |
default: |
775 |
default: |
| 776 |
const APInt *Offset = nullptr; |
776 |
const APInt *Offset = nullptr; |
| 777 |
if (match(Cmp, m_ICmp(Pred, m_CombineOr(m_V, m_Add(m_V, m_APInt(Offset))), |
777 |
if (match(Cmp, m_ICmp(Pred, m_CombineOr(m_V, m_Add(m_V, m_APInt(Offset))), |
| 778 |
m_Value(A)))) { |
778 |
m_Value(A)))) { |
| 779 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
779 |
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC); |
| 780 |
ConstantRange RHSRange = |
780 |
ConstantRange RHSRange = |
| 781 |
ConstantRange::fromKnownBits(RHSKnown, Cmp->isSigned()); |
781 |
ConstantRange::fromKnownBits(RHSKnown, Cmp->isSigned()); |
| 782 |
ConstantRange LHSRange = |
782 |
ConstantRange LHSRange = |
| 783 |
ConstantRange::makeAllowedICmpRegion(Pred, RHSRange); |
783 |
ConstantRange::makeAllowedICmpRegion(Pred, RHSRange); |
| 784 |
if (Offset) |
784 |
if (Offset) |
| 785 |
LHSRange = LHSRange.sub(*Offset); |
785 |
LHSRange = LHSRange.sub(*Offset); |
| 786 |
Known = Known.unionWith(LHSRange.toKnownBits()); |
786 |
Known = Known.unionWith(LHSRange.toKnownBits()); |
| 787 |
} |
787 |
} |
| 788 |
break; |
788 |
break; |
| 789 |
} |
789 |
} |
| 790 |
} |
790 |
} |
| 791 |
|
791 |
|
| 792 |
void llvm::computeKnownBitsFromAssume(const Value *V, KnownBits &Known, |
792 |
void llvm::computeKnownBitsFromAssume(const Value *V, KnownBits &Known, |
| 793 |
unsigned Depth, const SimplifyQuery &Q) { |
793 |
unsigned Depth, const SimplifyQuery &Q) { |
| 794 |
// Use of assumptions is context-sensitive. If we don't have a context, we |
794 |
// Use of assumptions is context-sensitive. If we don't have a context, we |
| 795 |
// cannot use them! |
795 |
// cannot use them! |
| 796 |
if (!Q.AC || !Q.CxtI) |
796 |
if (!Q.AC || !Q.CxtI) |
| 797 |
return; |
797 |
return; |
| 798 |
|
798 |
|
| 799 |
unsigned BitWidth = Known.getBitWidth(); |
799 |
unsigned BitWidth = Known.getBitWidth(); |
| 800 |
|
800 |
|
| 801 |
// Refine Known set if the pointer alignment is set by assume bundles. |
801 |
// Refine Known set if the pointer alignment is set by assume bundles. |
| 802 |
if (V->getType()->isPointerTy()) { |
802 |
if (V->getType()->isPointerTy()) { |
| 803 |
if (RetainedKnowledge RK = getKnowledgeValidInContext( |
803 |
if (RetainedKnowledge RK = getKnowledgeValidInContext( |
| 804 |
V, { Attribute::Alignment }, Q.CxtI, Q.DT, Q.AC)) { |
804 |
V, { Attribute::Alignment }, Q.CxtI, Q.DT, Q.AC)) { |
| 805 |
if (isPowerOf2_64(RK.ArgValue)) |
805 |
if (isPowerOf2_64(RK.ArgValue)) |
| 806 |
Known.Zero.setLowBits(Log2_64(RK.ArgValue)); |
806 |
Known.Zero.setLowBits(Log2_64(RK.ArgValue)); |
| 807 |
} |
807 |
} |
| 808 |
} |
808 |
} |
| 809 |
|
809 |
|
| 810 |
// Note that the patterns below need to be kept in sync with the code |
810 |
// Note that the patterns below need to be kept in sync with the code |
| 811 |
// in AssumptionCache::updateAffectedValues. |
811 |
// in AssumptionCache::updateAffectedValues. |
| 812 |
|
812 |
|
| 813 |
for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { |
813 |
for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { |
| 814 |
if (!AssumeVH) |
814 |
if (!AssumeVH) |
| 815 |
continue; |
815 |
continue; |
| 816 |
CallInst *I = cast(AssumeVH); |
816 |
CallInst *I = cast(AssumeVH); |
| 817 |
assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() && |
817 |
assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() && |
| 818 |
"Got assumption for the wrong function!"); |
818 |
"Got assumption for the wrong function!"); |
| 819 |
|
819 |
|
| 820 |
// Warning: This loop can end up being somewhat performance sensitive. |
820 |
// Warning: This loop can end up being somewhat performance sensitive. |
| 821 |
// We're running this loop for once for each value queried resulting in a |
821 |
// We're running this loop for once for each value queried resulting in a |
| 822 |
// runtime of ~O(#assumes * #values). |
822 |
// runtime of ~O(#assumes * #values). |
| 823 |
|
823 |
|
| 824 |
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
824 |
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
| 825 |
"must be an assume intrinsic"); |
825 |
"must be an assume intrinsic"); |
| 826 |
|
826 |
|
| 827 |
Value *Arg = I->getArgOperand(0); |
827 |
Value *Arg = I->getArgOperand(0); |
| 828 |
|
828 |
|
| 829 |
if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
829 |
if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
| 830 |
assert(BitWidth == 1 && "assume operand is not i1?"); |
830 |
assert(BitWidth == 1 && "assume operand is not i1?"); |
| 831 |
(void)BitWidth; |
831 |
(void)BitWidth; |
| 832 |
Known.setAllOnes(); |
832 |
Known.setAllOnes(); |
| 833 |
return; |
833 |
return; |
| 834 |
} |
834 |
} |
| 835 |
if (match(Arg, m_Not(m_Specific(V))) && |
835 |
if (match(Arg, m_Not(m_Specific(V))) && |
| 836 |
isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
836 |
isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
| 837 |
assert(BitWidth == 1 && "assume operand is not i1?"); |
837 |
assert(BitWidth == 1 && "assume operand is not i1?"); |
| 838 |
(void)BitWidth; |
838 |
(void)BitWidth; |
| 839 |
Known.setAllZero(); |
839 |
Known.setAllZero(); |
| 840 |
return; |
840 |
return; |
| 841 |
} |
841 |
} |
| 842 |
|
842 |
|
| 843 |
// The remaining tests are all recursive, so bail out if we hit the limit. |
843 |
// The remaining tests are all recursive, so bail out if we hit the limit. |
| 844 |
if (Depth == MaxAnalysisRecursionDepth) |
844 |
if (Depth == MaxAnalysisRecursionDepth) |
| 845 |
continue; |
845 |
continue; |
| 846 |
|
846 |
|
| 847 |
ICmpInst *Cmp = dyn_cast(Arg); |
847 |
ICmpInst *Cmp = dyn_cast(Arg); |
| 848 |
if (!Cmp) |
848 |
if (!Cmp) |
| 849 |
continue; |
849 |
continue; |
| 850 |
|
850 |
|
| 851 |
if (!isValidAssumeForContext(I, Q.CxtI, Q.DT)) |
851 |
if (!isValidAssumeForContext(I, Q.CxtI, Q.DT)) |
| 852 |
continue; |
852 |
continue; |
| 853 |
|
853 |
|
| 854 |
computeKnownBitsFromCmp(V, Cmp, Known, Depth, Q); |
854 |
computeKnownBitsFromCmp(V, Cmp, Known, Depth, Q); |
| 855 |
} |
855 |
} |
| 856 |
|
856 |
|
| 857 |
// Conflicting assumption: Undefined behavior will occur on this execution |
857 |
// Conflicting assumption: Undefined behavior will occur on this execution |
| 858 |
// path. |
858 |
// path. |
| 859 |
if (Known.hasConflict()) |
859 |
if (Known.hasConflict()) |
| 860 |
Known.resetAll(); |
860 |
Known.resetAll(); |
| 861 |
} |
861 |
} |
| 862 |
|
862 |
|
| 863 |
/// Compute known bits from a shift operator, including those with a |
863 |
/// Compute known bits from a shift operator, including those with a |
| 864 |
/// non-constant shift amount. Known is the output of this function. Known2 is a |
864 |
/// non-constant shift amount. Known is the output of this function. Known2 is a |
| 865 |
/// pre-allocated temporary with the same bit width as Known and on return |
865 |
/// pre-allocated temporary with the same bit width as Known and on return |
| 866 |
/// contains the known bit of the shift value source. KF is an |
866 |
/// contains the known bit of the shift value source. KF is an |
| 867 |
/// operator-specific function that, given the known-bits and a shift amount, |
867 |
/// operator-specific function that, given the known-bits and a shift amount, |
| 868 |
/// compute the implied known-bits of the shift operator's result respectively |
868 |
/// compute the implied known-bits of the shift operator's result respectively |
| 869 |
/// for that shift amount. The results from calling KF are conservatively |
869 |
/// for that shift amount. The results from calling KF are conservatively |
| 870 |
/// combined for all permitted shift amounts. |
870 |
/// combined for all permitted shift amounts. |
| 871 |
static void computeKnownBitsFromShiftOperator( |
871 |
static void computeKnownBitsFromShiftOperator( |
| 872 |
const Operator *I, const APInt &DemandedElts, KnownBits &Known, |
872 |
const Operator *I, const APInt &DemandedElts, KnownBits &Known, |
| 873 |
KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q, |
873 |
KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q, |
| 874 |
function_ref KF) { |
874 |
function_ref KF) { |
| 875 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
875 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
| 876 |
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); |
876 |
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); |
| 877 |
// To limit compile-time impact, only query isKnownNonZero() if we know at |
877 |
// To limit compile-time impact, only query isKnownNonZero() if we know at |
| 878 |
// least something about the shift amount. |
878 |
// least something about the shift amount. |
| 879 |
bool ShAmtNonZero = |
879 |
bool ShAmtNonZero = |
| 880 |
Known.isNonZero() || |
880 |
Known.isNonZero() || |
| 881 |
(Known.getMaxValue().ult(Known.getBitWidth()) && |
881 |
(Known.getMaxValue().ult(Known.getBitWidth()) && |
| 882 |
isKnownNonZero(I->getOperand(1), DemandedElts, Depth + 1, Q)); |
882 |
isKnownNonZero(I->getOperand(1), DemandedElts, Depth + 1, Q)); |
| 883 |
Known = KF(Known2, Known, ShAmtNonZero); |
883 |
Known = KF(Known2, Known, ShAmtNonZero); |
| 884 |
} |
884 |
} |
| 885 |
|
885 |
|
| 886 |
static KnownBits |
886 |
static KnownBits |
| 887 |
getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, |
887 |
getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, |
| 888 |
const KnownBits &KnownLHS, const KnownBits &KnownRHS, |
888 |
const KnownBits &KnownLHS, const KnownBits &KnownRHS, |
| 889 |
unsigned Depth, const SimplifyQuery &Q) { |
889 |
unsigned Depth, const SimplifyQuery &Q) { |
| 890 |
unsigned BitWidth = KnownLHS.getBitWidth(); |
890 |
unsigned BitWidth = KnownLHS.getBitWidth(); |
| 891 |
KnownBits KnownOut(BitWidth); |
891 |
KnownBits KnownOut(BitWidth); |
| 892 |
bool IsAnd = false; |
892 |
bool IsAnd = false; |
| 893 |
bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero(); |
893 |
bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero(); |
| 894 |
Value *X = nullptr, *Y = nullptr; |
894 |
Value *X = nullptr, *Y = nullptr; |
| 895 |
|
895 |
|
| 896 |
switch (I->getOpcode()) { |
896 |
switch (I->getOpcode()) { |
| 897 |
case Instruction::And: |
897 |
case Instruction::And: |
| 898 |
KnownOut = KnownLHS & KnownRHS; |
898 |
KnownOut = KnownLHS & KnownRHS; |
| 899 |
IsAnd = true; |
899 |
IsAnd = true; |
| 900 |
// and(x, -x) is common idioms that will clear all but lowest set |
900 |
// and(x, -x) is common idioms that will clear all but lowest set |
| 901 |
// bit. If we have a single known bit in x, we can clear all bits |
901 |
// bit. If we have a single known bit in x, we can clear all bits |
| 902 |
// above it. |
902 |
// above it. |
| 903 |
// TODO: instcombine often reassociates independent `and` which can hide |
903 |
// TODO: instcombine often reassociates independent `and` which can hide |
| 904 |
// this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x). |
904 |
// this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x). |
| 905 |
if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) { |
905 |
if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) { |
| 906 |
// -(-x) == x so using whichever (LHS/RHS) gets us a better result. |
906 |
// -(-x) == x so using whichever (LHS/RHS) gets us a better result. |
| 907 |
if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros()) |
907 |
if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros()) |
| 908 |
KnownOut = KnownLHS.blsi(); |
908 |
KnownOut = KnownLHS.blsi(); |
| 909 |
else |
909 |
else |
| 910 |
KnownOut = KnownRHS.blsi(); |
910 |
KnownOut = KnownRHS.blsi(); |
| 911 |
} |
911 |
} |
| 912 |
break; |
912 |
break; |
| 913 |
case Instruction::Or: |
913 |
case Instruction::Or: |
| 914 |
KnownOut = KnownLHS | KnownRHS; |
914 |
KnownOut = KnownLHS | KnownRHS; |
| 915 |
break; |
915 |
break; |
| 916 |
case Instruction::Xor: |
916 |
case Instruction::Xor: |
| 917 |
KnownOut = KnownLHS ^ KnownRHS; |
917 |
KnownOut = KnownLHS ^ KnownRHS; |
| 918 |
// xor(x, x-1) is common idioms that will clear all but lowest set |
918 |
// xor(x, x-1) is common idioms that will clear all but lowest set |
| 919 |
// bit. If we have a single known bit in x, we can clear all bits |
919 |
// bit. If we have a single known bit in x, we can clear all bits |
| 920 |
// above it. |
920 |
// above it. |
| 921 |
// TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C != |
921 |
// TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C != |
| 922 |
// -1 but for the purpose of demanded bits (xor(x, x-C) & |
922 |
// -1 but for the purpose of demanded bits (xor(x, x-C) & |
| 923 |
// Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern |
923 |
// Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern |
| 924 |
// to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1). |
924 |
// to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1). |
| 925 |
if (HasKnownOne && |
925 |
if (HasKnownOne && |
| 926 |
match(I, m_c_Xor(m_Value(X), m_c_Add(m_Deferred(X), m_AllOnes())))) { |
926 |
match(I, m_c_Xor(m_Value(X), m_c_Add(m_Deferred(X), m_AllOnes())))) { |
| 927 |
const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS; |
927 |
const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS; |
| 928 |
KnownOut = XBits.blsmsk(); |
928 |
KnownOut = XBits.blsmsk(); |
| 929 |
} |
929 |
} |
| 930 |
break; |
930 |
break; |
| 931 |
default: |
931 |
default: |
| 932 |
llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'"); |
932 |
llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'"); |
| 933 |
} |
933 |
} |
| 934 |
|
934 |
|
| 935 |
// and(x, add (x, -1)) is a common idiom that always clears the low bit; |
935 |
// and(x, add (x, -1)) is a common idiom that always clears the low bit; |
| 936 |
// xor/or(x, add (x, -1)) is an idiom that will always set the low bit. |
936 |
// xor/or(x, add (x, -1)) is an idiom that will always set the low bit. |
| 937 |
// here we handle the more general case of adding any odd number by |
937 |
// here we handle the more general case of adding any odd number by |
| 938 |
// matching the form and/xor/or(x, add(x, y)) where y is odd. |
938 |
// matching the form and/xor/or(x, add(x, y)) where y is odd. |
| 939 |
// TODO: This could be generalized to clearing any bit set in y where the |
939 |
// TODO: This could be generalized to clearing any bit set in y where the |
| 940 |
// following bit is known to be unset in y. |
940 |
// following bit is known to be unset in y. |
| 941 |
if (!KnownOut.Zero[0] && !KnownOut.One[0] && |
941 |
if (!KnownOut.Zero[0] && !KnownOut.One[0] && |
| 942 |
(match(I, m_c_BinOp(m_Value(X), m_c_Add(m_Deferred(X), m_Value(Y)))) || |
942 |
(match(I, m_c_BinOp(m_Value(X), m_c_Add(m_Deferred(X), m_Value(Y)))) || |
| 943 |
match(I, m_c_BinOp(m_Value(X), m_Sub(m_Deferred(X), m_Value(Y)))) || |
943 |
match(I, m_c_BinOp(m_Value(X), m_Sub(m_Deferred(X), m_Value(Y)))) || |
| 944 |
match(I, m_c_BinOp(m_Value(X), m_Sub(m_Value(Y), m_Deferred(X)))))) { |
944 |
match(I, m_c_BinOp(m_Value(X), m_Sub(m_Value(Y), m_Deferred(X)))))) { |
| 945 |
KnownBits KnownY(BitWidth); |
945 |
KnownBits KnownY(BitWidth); |
| 946 |
computeKnownBits(Y, DemandedElts, KnownY, Depth + 1, Q); |
946 |
computeKnownBits(Y, DemandedElts, KnownY, Depth + 1, Q); |
| 947 |
if (KnownY.countMinTrailingOnes() > 0) { |
947 |
if (KnownY.countMinTrailingOnes() > 0) { |
| 948 |
if (IsAnd) |
948 |
if (IsAnd) |
| 949 |
KnownOut.Zero.setBit(0); |
949 |
KnownOut.Zero.setBit(0); |
| 950 |
else |
950 |
else |
| 951 |
KnownOut.One.setBit(0); |
951 |
KnownOut.One.setBit(0); |
| 952 |
} |
952 |
} |
| 953 |
} |
953 |
} |
| 954 |
return KnownOut; |
954 |
return KnownOut; |
| 955 |
} |
955 |
} |
| 956 |
|
956 |
|
| 957 |
// Public so this can be used in `SimplifyDemandedUseBits`. |
957 |
// Public so this can be used in `SimplifyDemandedUseBits`. |
| 958 |
KnownBits llvm::analyzeKnownBitsFromAndXorOr( |
958 |
KnownBits llvm::analyzeKnownBitsFromAndXorOr( |
| 959 |
const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, |
959 |
const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, |
| 960 |
unsigned Depth, const DataLayout &DL, AssumptionCache *AC, |
960 |
unsigned Depth, const DataLayout &DL, AssumptionCache *AC, |
| 961 |
const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { |
961 |
const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { |
| 962 |
auto *FVTy = dyn_cast(I->getType()); |
962 |
auto *FVTy = dyn_cast(I->getType()); |
| 963 |
APInt DemandedElts = |
963 |
APInt DemandedElts = |
| 964 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
964 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
| 965 |
|
965 |
|
| 966 |
return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, Depth, |
966 |
return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, Depth, |
| 967 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
967 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, |
| 968 |
safeCxtI(I, CxtI), |
968 |
safeCxtI(I, CxtI), |
| 969 |
UseInstrInfo)); |
969 |
UseInstrInfo)); |
| 970 |
} |
970 |
} |
| 971 |
|
971 |
|
| 972 |
ConstantRange llvm::getVScaleRange(const Function *F, unsigned BitWidth) { |
972 |
ConstantRange llvm::getVScaleRange(const Function *F, unsigned BitWidth) { |
| 973 |
Attribute Attr = F->getFnAttribute(Attribute::VScaleRange); |
973 |
Attribute Attr = F->getFnAttribute(Attribute::VScaleRange); |
| 974 |
// Without vscale_range, we only know that vscale is non-zero. |
974 |
// Without vscale_range, we only know that vscale is non-zero. |
| 975 |
if (!Attr.isValid()) |
975 |
if (!Attr.isValid()) |
| 976 |
return ConstantRange(APInt(BitWidth, 1), APInt::getZero(BitWidth)); |
976 |
return ConstantRange(APInt(BitWidth, 1), APInt::getZero(BitWidth)); |
| 977 |
|
977 |
|
| 978 |
unsigned AttrMin = Attr.getVScaleRangeMin(); |
978 |
unsigned AttrMin = Attr.getVScaleRangeMin(); |
| 979 |
// Minimum is larger than vscale width, result is always poison. |
979 |
// Minimum is larger than vscale width, result is always poison. |
| 980 |
if ((unsigned)llvm::bit_width(AttrMin) > BitWidth) |
980 |
if ((unsigned)llvm::bit_width(AttrMin) > BitWidth) |
| 981 |
return ConstantRange::getEmpty(BitWidth); |
981 |
return ConstantRange::getEmpty(BitWidth); |
| 982 |
|
982 |
|
| 983 |
APInt Min(BitWidth, AttrMin); |
983 |
APInt Min(BitWidth, AttrMin); |
| 984 |
std::optional AttrMax = Attr.getVScaleRangeMax(); |
984 |
std::optional AttrMax = Attr.getVScaleRangeMax(); |
| 985 |
if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth) |
985 |
if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth) |
| 986 |
return ConstantRange(Min, APInt::getZero(BitWidth)); |
986 |
return ConstantRange(Min, APInt::getZero(BitWidth)); |
| 987 |
|
987 |
|
| 988 |
return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1); |
988 |
return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1); |
| 989 |
} |
989 |
} |
| 990 |
|
990 |
|
| 991 |
static void computeKnownBitsFromOperator(const Operator *I, |
991 |
static void computeKnownBitsFromOperator(const Operator *I, |
| 992 |
const APInt &DemandedElts, |
992 |
const APInt &DemandedElts, |
| 993 |
KnownBits &Known, unsigned Depth, |
993 |
KnownBits &Known, unsigned Depth, |
| 994 |
const SimplifyQuery &Q) { |
994 |
const SimplifyQuery &Q) { |
| 995 |
unsigned BitWidth = Known.getBitWidth(); |
995 |
unsigned BitWidth = Known.getBitWidth(); |
| 996 |
|
996 |
|
| 997 |
KnownBits Known2(BitWidth); |
997 |
KnownBits Known2(BitWidth); |
| 998 |
switch (I->getOpcode()) { |
998 |
switch (I->getOpcode()) { |
| 999 |
default: break; |
999 |
default: break; |
| 1000 |
case Instruction::Load: |
1000 |
case Instruction::Load: |
| 1001 |
if (MDNode *MD = |
1001 |
if (MDNode *MD = |
| 1002 |
Q.IIQ.getMetadata(cast(I), LLVMContext::MD_range)) |
1002 |
Q.IIQ.getMetadata(cast(I), LLVMContext::MD_range)) |
| 1003 |
computeKnownBitsFromRangeMetadata(*MD, Known); |
1003 |
computeKnownBitsFromRangeMetadata(*MD, Known); |
| 1004 |
break; |
1004 |
break; |
| 1005 |
case Instruction::And: |
1005 |
case Instruction::And: |
| 1006 |
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); |
1006 |
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); |
| 1007 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
1007 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
| 1008 |
|
1008 |
|
| 1009 |
Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q); |
1009 |
Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q); |
| 1010 |
break; |
1010 |
break; |
| 1011 |
case Instruction::Or: |
1011 |
case Instruction::Or: |
| 1012 |
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); |
1012 |
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); |
| 1013 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
1013 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
| 1014 |
|
1014 |
|
| 1015 |
Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q); |
1015 |
Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q); |
| 1016 |
break; |
1016 |
break; |
| 1017 |
case Instruction::Xor: |
1017 |
case Instruction::Xor: |
| 1018 |
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); |
1018 |
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q); |
| 1019 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
1019 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
| 1020 |
|
1020 |
|
| 1021 |
Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q); |
1021 |
Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q); |
| 1022 |
break; |
1022 |
break; |
| 1023 |
case Instruction::Mul: { |
1023 |
case Instruction::Mul: { |
| 1024 |
bool NSW = Q.IIQ.hasNoSignedWrap(cast(I)); |
1024 |
bool NSW = Q.IIQ.hasNoSignedWrap(cast(I)); |
| 1025 |
computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, DemandedElts, |
1025 |
computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, DemandedElts, |
| 1026 |
Known, Known2, Depth, Q); |
1026 |
Known, Known2, Depth, Q); |
| 1027 |
break; |
1027 |
break; |
| 1028 |
} |
1028 |
} |
| 1029 |
case Instruction::UDiv: { |
1029 |
case Instruction::UDiv: { |
| 1030 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1030 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1031 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1031 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1032 |
Known = |
1032 |
Known = |
| 1033 |
KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast(I))); |
1033 |
KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast(I))); |
| 1034 |
break; |
1034 |
break; |
| 1035 |
} |
1035 |
} |
| 1036 |
case Instruction::SDiv: { |
1036 |
case Instruction::SDiv: { |
| 1037 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1037 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1038 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1038 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1039 |
Known = |
1039 |
Known = |
| 1040 |
KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast(I))); |
1040 |
KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast(I))); |
| 1041 |
break; |
1041 |
break; |
| 1042 |
} |
1042 |
} |
| 1043 |
case Instruction::Select: { |
1043 |
case Instruction::Select: { |
| 1044 |
const Value *LHS = nullptr, *RHS = nullptr; |
1044 |
const Value *LHS = nullptr, *RHS = nullptr; |
| 1045 |
SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor; |
1045 |
SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor; |
| 1046 |
if (SelectPatternResult::isMinOrMax(SPF)) { |
1046 |
if (SelectPatternResult::isMinOrMax(SPF)) { |
| 1047 |
computeKnownBits(RHS, Known, Depth + 1, Q); |
1047 |
computeKnownBits(RHS, Known, Depth + 1, Q); |
| 1048 |
computeKnownBits(LHS, Known2, Depth + 1, Q); |
1048 |
computeKnownBits(LHS, Known2, Depth + 1, Q); |
| 1049 |
switch (SPF) { |
1049 |
switch (SPF) { |
| 1050 |
default: |
1050 |
default: |
| 1051 |
llvm_unreachable("Unhandled select pattern flavor!"); |
1051 |
llvm_unreachable("Unhandled select pattern flavor!"); |
| 1052 |
case SPF_SMAX: |
1052 |
case SPF_SMAX: |
| 1053 |
Known = KnownBits::smax(Known, Known2); |
1053 |
Known = KnownBits::smax(Known, Known2); |
| 1054 |
break; |
1054 |
break; |
| 1055 |
case SPF_SMIN: |
1055 |
case SPF_SMIN: |
| 1056 |
Known = KnownBits::smin(Known, Known2); |
1056 |
Known = KnownBits::smin(Known, Known2); |
| 1057 |
break; |
1057 |
break; |
| 1058 |
case SPF_UMAX: |
1058 |
case SPF_UMAX: |
| 1059 |
Known = KnownBits::umax(Known, Known2); |
1059 |
Known = KnownBits::umax(Known, Known2); |
| 1060 |
break; |
1060 |
break; |
| 1061 |
case SPF_UMIN: |
1061 |
case SPF_UMIN: |
| 1062 |
Known = KnownBits::umin(Known, Known2); |
1062 |
Known = KnownBits::umin(Known, Known2); |
| 1063 |
break; |
1063 |
break; |
| 1064 |
} |
1064 |
} |
| 1065 |
break; |
1065 |
break; |
| 1066 |
} |
1066 |
} |
| 1067 |
|
1067 |
|
| 1068 |
computeKnownBits(I->getOperand(2), Known, Depth + 1, Q); |
1068 |
computeKnownBits(I->getOperand(2), Known, Depth + 1, Q); |
| 1069 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1069 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1070 |
|
1070 |
|
| 1071 |
// Only known if known in both the LHS and RHS. |
1071 |
// Only known if known in both the LHS and RHS. |
| 1072 |
Known = Known.intersectWith(Known2); |
1072 |
Known = Known.intersectWith(Known2); |
| 1073 |
|
1073 |
|
| 1074 |
if (SPF == SPF_ABS) { |
1074 |
if (SPF == SPF_ABS) { |
| 1075 |
// RHS from matchSelectPattern returns the negation part of abs pattern. |
1075 |
// RHS from matchSelectPattern returns the negation part of abs pattern. |
| 1076 |
// If the negate has an NSW flag we can assume the sign bit of the result |
1076 |
// If the negate has an NSW flag we can assume the sign bit of the result |
| 1077 |
// will be 0 because that makes abs(INT_MIN) undefined. |
1077 |
// will be 0 because that makes abs(INT_MIN) undefined. |
| 1078 |
if (match(RHS, m_Neg(m_Specific(LHS))) && |
1078 |
if (match(RHS, m_Neg(m_Specific(LHS))) && |
| 1079 |
Q.IIQ.hasNoSignedWrap(cast(RHS))) |
1079 |
Q.IIQ.hasNoSignedWrap(cast(RHS))) |
| 1080 |
Known.Zero.setSignBit(); |
1080 |
Known.Zero.setSignBit(); |
| 1081 |
} |
1081 |
} |
| 1082 |
|
1082 |
|
| 1083 |
break; |
1083 |
break; |
| 1084 |
} |
1084 |
} |
| 1085 |
case Instruction::FPTrunc: |
1085 |
case Instruction::FPTrunc: |
| 1086 |
case Instruction::FPExt: |
1086 |
case Instruction::FPExt: |
| 1087 |
case Instruction::FPToUI: |
1087 |
case Instruction::FPToUI: |
| 1088 |
case Instruction::FPToSI: |
1088 |
case Instruction::FPToSI: |
| 1089 |
case Instruction::SIToFP: |
1089 |
case Instruction::SIToFP: |
| 1090 |
case Instruction::UIToFP: |
1090 |
case Instruction::UIToFP: |
| 1091 |
break; // Can't work with floating point. |
1091 |
break; // Can't work with floating point. |
| 1092 |
case Instruction::PtrToInt: |
1092 |
case Instruction::PtrToInt: |
| 1093 |
case Instruction::IntToPtr: |
1093 |
case Instruction::IntToPtr: |
| 1094 |
// Fall through and handle them the same as zext/trunc. |
1094 |
// Fall through and handle them the same as zext/trunc. |
| 1095 |
[[fallthrough]]; |
1095 |
[[fallthrough]]; |
| 1096 |
case Instruction::ZExt: |
1096 |
case Instruction::ZExt: |
| 1097 |
case Instruction::Trunc: { |
1097 |
case Instruction::Trunc: { |
| 1098 |
Type *SrcTy = I->getOperand(0)->getType(); |
1098 |
Type *SrcTy = I->getOperand(0)->getType(); |
| 1099 |
|
1099 |
|
| 1100 |
unsigned SrcBitWidth; |
1100 |
unsigned SrcBitWidth; |
| 1101 |
// Note that we handle pointer operands here because of inttoptr/ptrtoint |
1101 |
// Note that we handle pointer operands here because of inttoptr/ptrtoint |
| 1102 |
// which fall through here. |
1102 |
// which fall through here. |
| 1103 |
Type *ScalarTy = SrcTy->getScalarType(); |
1103 |
Type *ScalarTy = SrcTy->getScalarType(); |
| 1104 |
SrcBitWidth = ScalarTy->isPointerTy() ? |
1104 |
SrcBitWidth = ScalarTy->isPointerTy() ? |
| 1105 |
Q.DL.getPointerTypeSizeInBits(ScalarTy) : |
1105 |
Q.DL.getPointerTypeSizeInBits(ScalarTy) : |
| 1106 |
Q.DL.getTypeSizeInBits(ScalarTy); |
1106 |
Q.DL.getTypeSizeInBits(ScalarTy); |
| 1107 |
|
1107 |
|
| 1108 |
assert(SrcBitWidth && "SrcBitWidth can't be zero"); |
1108 |
assert(SrcBitWidth && "SrcBitWidth can't be zero"); |
| 1109 |
Known = Known.anyextOrTrunc(SrcBitWidth); |
1109 |
Known = Known.anyextOrTrunc(SrcBitWidth); |
| 1110 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1110 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1111 |
Known = Known.zextOrTrunc(BitWidth); |
1111 |
Known = Known.zextOrTrunc(BitWidth); |
| 1112 |
break; |
1112 |
break; |
| 1113 |
} |
1113 |
} |
| 1114 |
case Instruction::BitCast: { |
1114 |
case Instruction::BitCast: { |
| 1115 |
Type *SrcTy = I->getOperand(0)->getType(); |
1115 |
Type *SrcTy = I->getOperand(0)->getType(); |
| 1116 |
if (SrcTy->isIntOrPtrTy() && |
1116 |
if (SrcTy->isIntOrPtrTy() && |
| 1117 |
// TODO: For now, not handling conversions like: |
1117 |
// TODO: For now, not handling conversions like: |
| 1118 |
// (bitcast i64 %x to <2 x i32>) |
1118 |
// (bitcast i64 %x to <2 x i32>) |
| 1119 |
!I->getType()->isVectorTy()) { |
1119 |
!I->getType()->isVectorTy()) { |
| 1120 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1120 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1121 |
break; |
1121 |
break; |
| 1122 |
} |
1122 |
} |
| 1123 |
|
1123 |
|
| 1124 |
// Handle cast from vector integer type to scalar or vector integer. |
1124 |
// Handle cast from vector integer type to scalar or vector integer. |
| 1125 |
auto *SrcVecTy = dyn_cast(SrcTy); |
1125 |
auto *SrcVecTy = dyn_cast(SrcTy); |
| 1126 |
if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() || |
1126 |
if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() || |
| 1127 |
!I->getType()->isIntOrIntVectorTy() || |
1127 |
!I->getType()->isIntOrIntVectorTy() || |
| 1128 |
isa(I->getType())) |
1128 |
isa(I->getType())) |
| 1129 |
break; |
1129 |
break; |
| 1130 |
|
1130 |
|
| 1131 |
// Look through a cast from narrow vector elements to wider type. |
1131 |
// Look through a cast from narrow vector elements to wider type. |
| 1132 |
// Examples: v4i32 -> v2i64, v3i8 -> v24 |
1132 |
// Examples: v4i32 -> v2i64, v3i8 -> v24 |
| 1133 |
unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits(); |
1133 |
unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits(); |
| 1134 |
if (BitWidth % SubBitWidth == 0) { |
1134 |
if (BitWidth % SubBitWidth == 0) { |
| 1135 |
// Known bits are automatically intersected across demanded elements of a |
1135 |
// Known bits are automatically intersected across demanded elements of a |
| 1136 |
// vector. So for example, if a bit is computed as known zero, it must be |
1136 |
// vector. So for example, if a bit is computed as known zero, it must be |
| 1137 |
// zero across all demanded elements of the vector. |
1137 |
// zero across all demanded elements of the vector. |
| 1138 |
// |
1138 |
// |
| 1139 |
// For this bitcast, each demanded element of the output is sub-divided |
1139 |
// For this bitcast, each demanded element of the output is sub-divided |
| 1140 |
// across a set of smaller vector elements in the source vector. To get |
1140 |
// across a set of smaller vector elements in the source vector. To get |
| 1141 |
// the known bits for an entire element of the output, compute the known |
1141 |
// the known bits for an entire element of the output, compute the known |
| 1142 |
// bits for each sub-element sequentially. This is done by shifting the |
1142 |
// bits for each sub-element sequentially. This is done by shifting the |
| 1143 |
// one-set-bit demanded elements parameter across the sub-elements for |
1143 |
// one-set-bit demanded elements parameter across the sub-elements for |
| 1144 |
// consecutive calls to computeKnownBits. We are using the demanded |
1144 |
// consecutive calls to computeKnownBits. We are using the demanded |
| 1145 |
// elements parameter as a mask operator. |
1145 |
// elements parameter as a mask operator. |
| 1146 |
// |
1146 |
// |
| 1147 |
// The known bits of each sub-element are then inserted into place |
1147 |
// The known bits of each sub-element are then inserted into place |
| 1148 |
// (dependent on endian) to form the full result of known bits. |
1148 |
// (dependent on endian) to form the full result of known bits. |
| 1149 |
unsigned NumElts = DemandedElts.getBitWidth(); |
1149 |
unsigned NumElts = DemandedElts.getBitWidth(); |
| 1150 |
unsigned SubScale = BitWidth / SubBitWidth; |
1150 |
unsigned SubScale = BitWidth / SubBitWidth; |
| 1151 |
APInt SubDemandedElts = APInt::getZero(NumElts * SubScale); |
1151 |
APInt SubDemandedElts = APInt::getZero(NumElts * SubScale); |
| 1152 |
for (unsigned i = 0; i != NumElts; ++i) { |
1152 |
for (unsigned i = 0; i != NumElts; ++i) { |
| 1153 |
if (DemandedElts[i]) |
1153 |
if (DemandedElts[i]) |
| 1154 |
SubDemandedElts.setBit(i * SubScale); |
1154 |
SubDemandedElts.setBit(i * SubScale); |
| 1155 |
} |
1155 |
} |
| 1156 |
|
1156 |
|
| 1157 |
KnownBits KnownSrc(SubBitWidth); |
1157 |
KnownBits KnownSrc(SubBitWidth); |
| 1158 |
for (unsigned i = 0; i != SubScale; ++i) { |
1158 |
for (unsigned i = 0; i != SubScale; ++i) { |
| 1159 |
computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, |
1159 |
computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, |
| 1160 |
Depth + 1, Q); |
1160 |
Depth + 1, Q); |
| 1161 |
unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i; |
1161 |
unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i; |
| 1162 |
Known.insertBits(KnownSrc, ShiftElt * SubBitWidth); |
1162 |
Known.insertBits(KnownSrc, ShiftElt * SubBitWidth); |
| 1163 |
} |
1163 |
} |
| 1164 |
} |
1164 |
} |
| 1165 |
break; |
1165 |
break; |
| 1166 |
} |
1166 |
} |
| 1167 |
case Instruction::SExt: { |
1167 |
case Instruction::SExt: { |
| 1168 |
// Compute the bits in the result that are not present in the input. |
1168 |
// Compute the bits in the result that are not present in the input. |
| 1169 |
unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); |
1169 |
unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); |
| 1170 |
|
1170 |
|
| 1171 |
Known = Known.trunc(SrcBitWidth); |
1171 |
Known = Known.trunc(SrcBitWidth); |
| 1172 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1172 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1173 |
// If the sign bit of the input is known set or clear, then we know the |
1173 |
// If the sign bit of the input is known set or clear, then we know the |
| 1174 |
// top bits of the result. |
1174 |
// top bits of the result. |
| 1175 |
Known = Known.sext(BitWidth); |
1175 |
Known = Known.sext(BitWidth); |
| 1176 |
break; |
1176 |
break; |
| 1177 |
} |
1177 |
} |
| 1178 |
case Instruction::Shl: { |
1178 |
case Instruction::Shl: { |
| 1179 |
bool NUW = Q.IIQ.hasNoUnsignedWrap(cast(I)); |
1179 |
bool NUW = Q.IIQ.hasNoUnsignedWrap(cast(I)); |
| 1180 |
bool NSW = Q.IIQ.hasNoSignedWrap(cast(I)); |
1180 |
bool NSW = Q.IIQ.hasNoSignedWrap(cast(I)); |
| 1181 |
auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt, |
1181 |
auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt, |
| 1182 |
bool ShAmtNonZero) { |
1182 |
bool ShAmtNonZero) { |
| 1183 |
return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero); |
1183 |
return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero); |
| 1184 |
}; |
1184 |
}; |
| 1185 |
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, |
1185 |
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, |
| 1186 |
KF); |
1186 |
KF); |
| 1187 |
// Trailing zeros of a right-shifted constant never decrease. |
1187 |
// Trailing zeros of a right-shifted constant never decrease. |
| 1188 |
const APInt *C; |
1188 |
const APInt *C; |
| 1189 |
if (match(I->getOperand(0), m_APInt(C))) |
1189 |
if (match(I->getOperand(0), m_APInt(C))) |
| 1190 |
Known.Zero.setLowBits(C->countr_zero()); |
1190 |
Known.Zero.setLowBits(C->countr_zero()); |
| 1191 |
break; |
1191 |
break; |
| 1192 |
} |
1192 |
} |
| 1193 |
case Instruction::LShr: { |
1193 |
case Instruction::LShr: { |
| 1194 |
auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt, |
1194 |
auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt, |
| 1195 |
bool ShAmtNonZero) { |
1195 |
bool ShAmtNonZero) { |
| 1196 |
return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero); |
1196 |
return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero); |
| 1197 |
}; |
1197 |
}; |
| 1198 |
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, |
1198 |
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, |
| 1199 |
KF); |
1199 |
KF); |
| 1200 |
// Leading zeros of a left-shifted constant never decrease. |
1200 |
// Leading zeros of a left-shifted constant never decrease. |
| 1201 |
const APInt *C; |
1201 |
const APInt *C; |
| 1202 |
if (match(I->getOperand(0), m_APInt(C))) |
1202 |
if (match(I->getOperand(0), m_APInt(C))) |
| 1203 |
Known.Zero.setHighBits(C->countl_zero()); |
1203 |
Known.Zero.setHighBits(C->countl_zero()); |
| 1204 |
break; |
1204 |
break; |
| 1205 |
} |
1205 |
} |
| 1206 |
case Instruction::AShr: { |
1206 |
case Instruction::AShr: { |
| 1207 |
auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt, |
1207 |
auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt, |
| 1208 |
bool ShAmtNonZero) { |
1208 |
bool ShAmtNonZero) { |
| 1209 |
return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero); |
1209 |
return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero); |
| 1210 |
}; |
1210 |
}; |
| 1211 |
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, |
1211 |
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q, |
| 1212 |
KF); |
1212 |
KF); |
| 1213 |
break; |
1213 |
break; |
| 1214 |
} |
1214 |
} |
| 1215 |
case Instruction::Sub: { |
1215 |
case Instruction::Sub: { |
| 1216 |
bool NSW = Q.IIQ.hasNoSignedWrap(cast(I)); |
1216 |
bool NSW = Q.IIQ.hasNoSignedWrap(cast(I)); |
| 1217 |
computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, |
1217 |
computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, |
| 1218 |
DemandedElts, Known, Known2, Depth, Q); |
1218 |
DemandedElts, Known, Known2, Depth, Q); |
| 1219 |
break; |
1219 |
break; |
| 1220 |
} |
1220 |
} |
| 1221 |
case Instruction::Add: { |
1221 |
case Instruction::Add: { |
| 1222 |
bool NSW = Q.IIQ.hasNoSignedWrap(cast(I)); |
1222 |
bool NSW = Q.IIQ.hasNoSignedWrap(cast(I)); |
| 1223 |
computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, |
1223 |
computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, |
| 1224 |
DemandedElts, Known, Known2, Depth, Q); |
1224 |
DemandedElts, Known, Known2, Depth, Q); |
| 1225 |
break; |
1225 |
break; |
| 1226 |
} |
1226 |
} |
| 1227 |
case Instruction::SRem: |
1227 |
case Instruction::SRem: |
| 1228 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1228 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1229 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1229 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1230 |
Known = KnownBits::srem(Known, Known2); |
1230 |
Known = KnownBits::srem(Known, Known2); |
| 1231 |
break; |
1231 |
break; |
| 1232 |
|
1232 |
|
| 1233 |
case Instruction::URem: |
1233 |
case Instruction::URem: |
| 1234 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1234 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1235 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1235 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1236 |
Known = KnownBits::urem(Known, Known2); |
1236 |
Known = KnownBits::urem(Known, Known2); |
| 1237 |
break; |
1237 |
break; |
| 1238 |
case Instruction::Alloca: |
1238 |
case Instruction::Alloca: |
| 1239 |
Known.Zero.setLowBits(Log2(cast(I)->getAlign())); |
1239 |
Known.Zero.setLowBits(Log2(cast(I)->getAlign())); |
| 1240 |
break; |
1240 |
break; |
| 1241 |
case Instruction::GetElementPtr: { |
1241 |
case Instruction::GetElementPtr: { |
| 1242 |
// Analyze all of the subscripts of this getelementptr instruction |
1242 |
// Analyze all of the subscripts of this getelementptr instruction |
| 1243 |
// to determine if we can prove known low zero bits. |
1243 |
// to determine if we can prove known low zero bits. |
| 1244 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1244 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1245 |
// Accumulate the constant indices in a separate variable |
1245 |
// Accumulate the constant indices in a separate variable |
| 1246 |
// to minimize the number of calls to computeForAddSub. |
1246 |
// to minimize the number of calls to computeForAddSub. |
| 1247 |
APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true); |
1247 |
APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true); |
| 1248 |
|
1248 |
|
| 1249 |
gep_type_iterator GTI = gep_type_begin(I); |
1249 |
gep_type_iterator GTI = gep_type_begin(I); |
| 1250 |
for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
1250 |
for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
| 1251 |
// TrailZ can only become smaller, short-circuit if we hit zero. |
1251 |
// TrailZ can only become smaller, short-circuit if we hit zero. |
| 1252 |
if (Known.isUnknown()) |
1252 |
if (Known.isUnknown()) |
| 1253 |
break; |
1253 |
break; |
| 1254 |
|
1254 |
|
| 1255 |
Value *Index = I->getOperand(i); |
1255 |
Value *Index = I->getOperand(i); |
| 1256 |
|
1256 |
|
| 1257 |
// Handle case when index is zero. |
1257 |
// Handle case when index is zero. |
| 1258 |
Constant *CIndex = dyn_cast(Index); |
1258 |
Constant *CIndex = dyn_cast(Index); |
| 1259 |
if (CIndex && CIndex->isZeroValue()) |
1259 |
if (CIndex && CIndex->isZeroValue()) |
| 1260 |
continue; |
1260 |
continue; |
| 1261 |
|
1261 |
|
| 1262 |
if (StructType *STy = GTI.getStructTypeOrNull()) { |
1262 |
if (StructType *STy = GTI.getStructTypeOrNull()) { |
| 1263 |
// Handle struct member offset arithmetic. |
1263 |
// Handle struct member offset arithmetic. |
| 1264 |
|
1264 |
|
| 1265 |
assert(CIndex && |
1265 |
assert(CIndex && |
| 1266 |
"Access to structure field must be known at compile time"); |
1266 |
"Access to structure field must be known at compile time"); |
| 1267 |
|
1267 |
|
| 1268 |
if (CIndex->getType()->isVectorTy()) |
1268 |
if (CIndex->getType()->isVectorTy()) |
| 1269 |
Index = CIndex->getSplatValue(); |
1269 |
Index = CIndex->getSplatValue(); |
| 1270 |
|
1270 |
|
| 1271 |
unsigned Idx = cast(Index)->getZExtValue(); |
1271 |
unsigned Idx = cast(Index)->getZExtValue(); |
| 1272 |
const StructLayout *SL = Q.DL.getStructLayout(STy); |
1272 |
const StructLayout *SL = Q.DL.getStructLayout(STy); |
| 1273 |
uint64_t Offset = SL->getElementOffset(Idx); |
1273 |
uint64_t Offset = SL->getElementOffset(Idx); |
| 1274 |
AccConstIndices += Offset; |
1274 |
AccConstIndices += Offset; |
| 1275 |
continue; |
1275 |
continue; |
| 1276 |
} |
1276 |
} |
| 1277 |
|
1277 |
|
| 1278 |
// Handle array index arithmetic. |
1278 |
// Handle array index arithmetic. |
| 1279 |
Type *IndexedTy = GTI.getIndexedType(); |
1279 |
Type *IndexedTy = GTI.getIndexedType(); |
| 1280 |
if (!IndexedTy->isSized()) { |
1280 |
if (!IndexedTy->isSized()) { |
| 1281 |
Known.resetAll(); |
1281 |
Known.resetAll(); |
| 1282 |
break; |
1282 |
break; |
| 1283 |
} |
1283 |
} |
| 1284 |
|
1284 |
|
| 1285 |
unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits(); |
1285 |
unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits(); |
| 1286 |
KnownBits IndexBits(IndexBitWidth); |
1286 |
KnownBits IndexBits(IndexBitWidth); |
| 1287 |
computeKnownBits(Index, IndexBits, Depth + 1, Q); |
1287 |
computeKnownBits(Index, IndexBits, Depth + 1, Q); |
| 1288 |
TypeSize IndexTypeSize = Q.DL.getTypeAllocSize(IndexedTy); |
1288 |
TypeSize IndexTypeSize = Q.DL.getTypeAllocSize(IndexedTy); |
| 1289 |
uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue(); |
1289 |
uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue(); |
| 1290 |
KnownBits ScalingFactor(IndexBitWidth); |
1290 |
KnownBits ScalingFactor(IndexBitWidth); |
| 1291 |
// Multiply by current sizeof type. |
1291 |
// Multiply by current sizeof type. |
| 1292 |
// &A[i] == A + i * sizeof(*A[i]). |
1292 |
// &A[i] == A + i * sizeof(*A[i]). |
| 1293 |
if (IndexTypeSize.isScalable()) { |
1293 |
if (IndexTypeSize.isScalable()) { |
| 1294 |
// For scalable types the only thing we know about sizeof is |
1294 |
// For scalable types the only thing we know about sizeof is |
| 1295 |
// that this is a multiple of the minimum size. |
1295 |
// that this is a multiple of the minimum size. |
| 1296 |
ScalingFactor.Zero.setLowBits(llvm::countr_zero(TypeSizeInBytes)); |
1296 |
ScalingFactor.Zero.setLowBits(llvm::countr_zero(TypeSizeInBytes)); |
| 1297 |
} else if (IndexBits.isConstant()) { |
1297 |
} else if (IndexBits.isConstant()) { |
| 1298 |
APInt IndexConst = IndexBits.getConstant(); |
1298 |
APInt IndexConst = IndexBits.getConstant(); |
| 1299 |
APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes); |
1299 |
APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes); |
| 1300 |
IndexConst *= ScalingFactor; |
1300 |
IndexConst *= ScalingFactor; |
| 1301 |
AccConstIndices += IndexConst.sextOrTrunc(BitWidth); |
1301 |
AccConstIndices += IndexConst.sextOrTrunc(BitWidth); |
| 1302 |
continue; |
1302 |
continue; |
| 1303 |
} else { |
1303 |
} else { |
| 1304 |
ScalingFactor = |
1304 |
ScalingFactor = |
| 1305 |
KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes)); |
1305 |
KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes)); |
| 1306 |
} |
1306 |
} |
| 1307 |
IndexBits = KnownBits::mul(IndexBits, ScalingFactor); |
1307 |
IndexBits = KnownBits::mul(IndexBits, ScalingFactor); |
| 1308 |
|
1308 |
|
| 1309 |
// If the offsets have a different width from the pointer, according |
1309 |
// If the offsets have a different width from the pointer, according |
| 1310 |
// to the language reference we need to sign-extend or truncate them |
1310 |
// to the language reference we need to sign-extend or truncate them |
| 1311 |
// to the width of the pointer. |
1311 |
// to the width of the pointer. |
| 1312 |
IndexBits = IndexBits.sextOrTrunc(BitWidth); |
1312 |
IndexBits = IndexBits.sextOrTrunc(BitWidth); |
| 1313 |
|
1313 |
|
| 1314 |
// Note that inbounds does *not* guarantee nsw for the addition, as only |
1314 |
// Note that inbounds does *not* guarantee nsw for the addition, as only |
| 1315 |
// the offset is signed, while the base address is unsigned. |
1315 |
// the offset is signed, while the base address is unsigned. |
| 1316 |
Known = KnownBits::computeForAddSub( |
1316 |
Known = KnownBits::computeForAddSub( |
| 1317 |
/*Add=*/true, /*NSW=*/false, Known, IndexBits); |
1317 |
/*Add=*/true, /*NSW=*/false, Known, IndexBits); |
| 1318 |
} |
1318 |
} |
| 1319 |
if (!Known.isUnknown() && !AccConstIndices.isZero()) { |
1319 |
if (!Known.isUnknown() && !AccConstIndices.isZero()) { |
| 1320 |
KnownBits Index = KnownBits::makeConstant(AccConstIndices); |
1320 |
KnownBits Index = KnownBits::makeConstant(AccConstIndices); |
| 1321 |
Known = KnownBits::computeForAddSub( |
1321 |
Known = KnownBits::computeForAddSub( |
| 1322 |
/*Add=*/true, /*NSW=*/false, Known, Index); |
1322 |
/*Add=*/true, /*NSW=*/false, Known, Index); |
| 1323 |
} |
1323 |
} |
| 1324 |
break; |
1324 |
break; |
| 1325 |
} |
1325 |
} |
| 1326 |
case Instruction::PHI: { |
1326 |
case Instruction::PHI: { |
| 1327 |
const PHINode *P = cast(I); |
1327 |
const PHINode *P = cast(I); |
| 1328 |
BinaryOperator *BO = nullptr; |
1328 |
BinaryOperator *BO = nullptr; |
| 1329 |
Value *R = nullptr, *L = nullptr; |
1329 |
Value *R = nullptr, *L = nullptr; |
| 1330 |
if (matchSimpleRecurrence(P, BO, R, L)) { |
1330 |
if (matchSimpleRecurrence(P, BO, R, L)) { |
| 1331 |
// Handle the case of a simple two-predecessor recurrence PHI. |
1331 |
// Handle the case of a simple two-predecessor recurrence PHI. |
| 1332 |
// There's a lot more that could theoretically be done here, but |
1332 |
// There's a lot more that could theoretically be done here, but |
| 1333 |
// this is sufficient to catch some interesting cases. |
1333 |
// this is sufficient to catch some interesting cases. |
| 1334 |
unsigned Opcode = BO->getOpcode(); |
1334 |
unsigned Opcode = BO->getOpcode(); |
| 1335 |
|
1335 |
|
| 1336 |
// If this is a shift recurrence, we know the bits being shifted in. |
1336 |
// If this is a shift recurrence, we know the bits being shifted in. |
| 1337 |
// We can combine that with information about the start value of the |
1337 |
// We can combine that with information about the start value of the |
| 1338 |
// recurrence to conclude facts about the result. |
1338 |
// recurrence to conclude facts about the result. |
| 1339 |
if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr || |
1339 |
if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr || |
| 1340 |
Opcode == Instruction::Shl) && |
1340 |
Opcode == Instruction::Shl) && |
| 1341 |
BO->getOperand(0) == I) { |
1341 |
BO->getOperand(0) == I) { |
| 1342 |
|
1342 |
|
| 1343 |
// We have matched a recurrence of the form: |
1343 |
// We have matched a recurrence of the form: |
| 1344 |
// %iv = [R, %entry], [%iv.next, %backedge] |
1344 |
// %iv = [R, %entry], [%iv.next, %backedge] |
| 1345 |
// %iv.next = shift_op %iv, L |
1345 |
// %iv.next = shift_op %iv, L |
| 1346 |
|
1346 |
|
| 1347 |
// Recurse with the phi context to avoid concern about whether facts |
1347 |
// Recurse with the phi context to avoid concern about whether facts |
| 1348 |
// inferred hold at original context instruction. TODO: It may be |
1348 |
// inferred hold at original context instruction. TODO: It may be |
| 1349 |
// correct to use the original context. IF warranted, explore and |
1349 |
// correct to use the original context. IF warranted, explore and |
| 1350 |
// add sufficient tests to cover. |
1350 |
// add sufficient tests to cover. |
| 1351 |
SimplifyQuery RecQ = Q; |
1351 |
SimplifyQuery RecQ = Q; |
| 1352 |
RecQ.CxtI = P; |
1352 |
RecQ.CxtI = P; |
| 1353 |
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ); |
1353 |
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ); |
| 1354 |
switch (Opcode) { |
1354 |
switch (Opcode) { |
| 1355 |
case Instruction::Shl: |
1355 |
case Instruction::Shl: |
| 1356 |
// A shl recurrence will only increase the tailing zeros |
1356 |
// A shl recurrence will only increase the tailing zeros |
| 1357 |
Known.Zero.setLowBits(Known2.countMinTrailingZeros()); |
1357 |
Known.Zero.setLowBits(Known2.countMinTrailingZeros()); |
| 1358 |
break; |
1358 |
break; |
| 1359 |
case Instruction::LShr: |
1359 |
case Instruction::LShr: |
| 1360 |
// A lshr recurrence will preserve the leading zeros of the |
1360 |
// A lshr recurrence will preserve the leading zeros of the |
| 1361 |
// start value |
1361 |
// start value |
| 1362 |
Known.Zero.setHighBits(Known2.countMinLeadingZeros()); |
1362 |
Known.Zero.setHighBits(Known2.countMinLeadingZeros()); |
| 1363 |
break; |
1363 |
break; |
| 1364 |
case Instruction::AShr: |
1364 |
case Instruction::AShr: |
| 1365 |
// An ashr recurrence will extend the initial sign bit |
1365 |
// An ashr recurrence will extend the initial sign bit |
| 1366 |
Known.Zero.setHighBits(Known2.countMinLeadingZeros()); |
1366 |
Known.Zero.setHighBits(Known2.countMinLeadingZeros()); |
| 1367 |
Known.One.setHighBits(Known2.countMinLeadingOnes()); |
1367 |
Known.One.setHighBits(Known2.countMinLeadingOnes()); |
| 1368 |
break; |
1368 |
break; |
| 1369 |
}; |
1369 |
}; |
| 1370 |
} |
1370 |
} |
| 1371 |
|
1371 |
|
| 1372 |
// Check for operations that have the property that if |
1372 |
// Check for operations that have the property that if |
| 1373 |
// both their operands have low zero bits, the result |
1373 |
// both their operands have low zero bits, the result |
| 1374 |
// will have low zero bits. |
1374 |
// will have low zero bits. |
| 1375 |
if (Opcode == Instruction::Add || |
1375 |
if (Opcode == Instruction::Add || |
| 1376 |
Opcode == Instruction::Sub || |
1376 |
Opcode == Instruction::Sub || |
| 1377 |
Opcode == Instruction::And || |
1377 |
Opcode == Instruction::And || |
| 1378 |
Opcode == Instruction::Or || |
1378 |
Opcode == Instruction::Or || |
| 1379 |
Opcode == Instruction::Mul) { |
1379 |
Opcode == Instruction::Mul) { |
| 1380 |
// Change the context instruction to the "edge" that flows into the |
1380 |
// Change the context instruction to the "edge" that flows into the |
| 1381 |
// phi. This is important because that is where the value is actually |
1381 |
// phi. This is important because that is where the value is actually |
| 1382 |
// "evaluated" even though it is used later somewhere else. (see also |
1382 |
// "evaluated" even though it is used later somewhere else. (see also |
| 1383 |
// D69571). |
1383 |
// D69571). |
| 1384 |
SimplifyQuery RecQ = Q; |
1384 |
SimplifyQuery RecQ = Q; |
| 1385 |
|
1385 |
|
| 1386 |
unsigned OpNum = P->getOperand(0) == R ? 0 : 1; |
1386 |
unsigned OpNum = P->getOperand(0) == R ? 0 : 1; |
| 1387 |
Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator(); |
1387 |
Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator(); |
| 1388 |
Instruction *LInst = P->getIncomingBlock(1-OpNum)->getTerminator(); |
1388 |
Instruction *LInst = P->getIncomingBlock(1-OpNum)->getTerminator(); |
| 1389 |
|
1389 |
|
| 1390 |
// Ok, we have a PHI of the form L op= R. Check for low |
1390 |
// Ok, we have a PHI of the form L op= R. Check for low |
| 1391 |
// zero bits. |
1391 |
// zero bits. |
| 1392 |
RecQ.CxtI = RInst; |
1392 |
RecQ.CxtI = RInst; |
| 1393 |
computeKnownBits(R, Known2, Depth + 1, RecQ); |
1393 |
computeKnownBits(R, Known2, Depth + 1, RecQ); |
| 1394 |
|
1394 |
|
| 1395 |
// We need to take the minimum number of known bits |
1395 |
// We need to take the minimum number of known bits |
| 1396 |
KnownBits Known3(BitWidth); |
1396 |
KnownBits Known3(BitWidth); |
| 1397 |
RecQ.CxtI = LInst; |
1397 |
RecQ.CxtI = LInst; |
| 1398 |
computeKnownBits(L, Known3, Depth + 1, RecQ); |
1398 |
computeKnownBits(L, Known3, Depth + 1, RecQ); |
| 1399 |
|
1399 |
|
| 1400 |
Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(), |
1400 |
Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(), |
| 1401 |
Known3.countMinTrailingZeros())); |
1401 |
Known3.countMinTrailingZeros())); |
| 1402 |
|
1402 |
|
| 1403 |
auto *OverflowOp = dyn_cast(BO); |
1403 |
auto *OverflowOp = dyn_cast(BO); |
| 1404 |
if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) { |
1404 |
if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) { |
| 1405 |
// If initial value of recurrence is nonnegative, and we are adding |
1405 |
// If initial value of recurrence is nonnegative, and we are adding |
| 1406 |
// a nonnegative number with nsw, the result can only be nonnegative |
1406 |
// a nonnegative number with nsw, the result can only be nonnegative |
| 1407 |
// or poison value regardless of the number of times we execute the |
1407 |
// or poison value regardless of the number of times we execute the |
| 1408 |
// add in phi recurrence. If initial value is negative and we are |
1408 |
// add in phi recurrence. If initial value is negative and we are |
| 1409 |
// adding a negative number with nsw, the result can only be |
1409 |
// adding a negative number with nsw, the result can only be |
| 1410 |
// negative or poison value. Similar arguments apply to sub and mul. |
1410 |
// negative or poison value. Similar arguments apply to sub and mul. |
| 1411 |
// |
1411 |
// |
| 1412 |
// (add non-negative, non-negative) --> non-negative |
1412 |
// (add non-negative, non-negative) --> non-negative |
| 1413 |
// (add negative, negative) --> negative |
1413 |
// (add negative, negative) --> negative |
| 1414 |
if (Opcode == Instruction::Add) { |
1414 |
if (Opcode == Instruction::Add) { |
| 1415 |
if (Known2.isNonNegative() && Known3.isNonNegative()) |
1415 |
if (Known2.isNonNegative() && Known3.isNonNegative()) |
| 1416 |
Known.makeNonNegative(); |
1416 |
Known.makeNonNegative(); |
| 1417 |
else if (Known2.isNegative() && Known3.isNegative()) |
1417 |
else if (Known2.isNegative() && Known3.isNegative()) |
| 1418 |
Known.makeNegative(); |
1418 |
Known.makeNegative(); |
| 1419 |
} |
1419 |
} |
| 1420 |
|
1420 |
|
| 1421 |
// (sub nsw non-negative, negative) --> non-negative |
1421 |
// (sub nsw non-negative, negative) --> non-negative |
| 1422 |
// (sub nsw negative, non-negative) --> negative |
1422 |
// (sub nsw negative, non-negative) --> negative |
| 1423 |
else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) { |
1423 |
else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) { |
| 1424 |
if (Known2.isNonNegative() && Known3.isNegative()) |
1424 |
if (Known2.isNonNegative() && Known3.isNegative()) |
| 1425 |
Known.makeNonNegative(); |
1425 |
Known.makeNonNegative(); |
| 1426 |
else if (Known2.isNegative() && Known3.isNonNegative()) |
1426 |
else if (Known2.isNegative() && Known3.isNonNegative()) |
| 1427 |
Known.makeNegative(); |
1427 |
Known.makeNegative(); |
| 1428 |
} |
1428 |
} |
| 1429 |
|
1429 |
|
| 1430 |
// (mul nsw non-negative, non-negative) --> non-negative |
1430 |
// (mul nsw non-negative, non-negative) --> non-negative |
| 1431 |
else if (Opcode == Instruction::Mul && Known2.isNonNegative() && |
1431 |
else if (Opcode == Instruction::Mul && Known2.isNonNegative() && |
| 1432 |
Known3.isNonNegative()) |
1432 |
Known3.isNonNegative()) |
| 1433 |
Known.makeNonNegative(); |
1433 |
Known.makeNonNegative(); |
| 1434 |
} |
1434 |
} |
| 1435 |
|
1435 |
|
| 1436 |
break; |
1436 |
break; |
| 1437 |
} |
1437 |
} |
| 1438 |
} |
1438 |
} |
| 1439 |
|
1439 |
|
| 1440 |
// Unreachable blocks may have zero-operand PHI nodes. |
1440 |
// Unreachable blocks may have zero-operand PHI nodes. |
| 1441 |
if (P->getNumIncomingValues() == 0) |
1441 |
if (P->getNumIncomingValues() == 0) |
| 1442 |
break; |
1442 |
break; |
| 1443 |
|
1443 |
|
| 1444 |
// Otherwise take the unions of the known bit sets of the operands, |
1444 |
// Otherwise take the unions of the known bit sets of the operands, |
| 1445 |
// taking conservative care to avoid excessive recursion. |
1445 |
// taking conservative care to avoid excessive recursion. |
| 1446 |
if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) { |
1446 |
if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) { |
| 1447 |
// Skip if every incoming value references to ourself. |
1447 |
// Skip if every incoming value references to ourself. |
| 1448 |
if (isa_and_nonnull(P->hasConstantValue())) |
1448 |
if (isa_and_nonnull(P->hasConstantValue())) |
| 1449 |
break; |
1449 |
break; |
| 1450 |
|
1450 |
|
| 1451 |
Known.Zero.setAllBits(); |
1451 |
Known.Zero.setAllBits(); |
| 1452 |
Known.One.setAllBits(); |
1452 |
Known.One.setAllBits(); |
| 1453 |
for (unsigned u = 0, e = P->getNumIncomingValues(); u < e; ++u) { |
1453 |
for (unsigned u = 0, e = P->getNumIncomingValues(); u < e; ++u) { |
| 1454 |
Value *IncValue = P->getIncomingValue(u); |
1454 |
Value *IncValue = P->getIncomingValue(u); |
| 1455 |
// Skip direct self references. |
1455 |
// Skip direct self references. |
| 1456 |
if (IncValue == P) continue; |
1456 |
if (IncValue == P) continue; |
| 1457 |
|
1457 |
|
| 1458 |
// Change the context instruction to the "edge" that flows into the |
1458 |
// Change the context instruction to the "edge" that flows into the |
| 1459 |
// phi. This is important because that is where the value is actually |
1459 |
// phi. This is important because that is where the value is actually |
| 1460 |
// "evaluated" even though it is used later somewhere else. (see also |
1460 |
// "evaluated" even though it is used later somewhere else. (see also |
| 1461 |
// D69571). |
1461 |
// D69571). |
| 1462 |
SimplifyQuery RecQ = Q; |
1462 |
SimplifyQuery RecQ = Q; |
| 1463 |
RecQ.CxtI = P->getIncomingBlock(u)->getTerminator(); |
1463 |
RecQ.CxtI = P->getIncomingBlock(u)->getTerminator(); |
| 1464 |
|
1464 |
|
| 1465 |
Known2 = KnownBits(BitWidth); |
1465 |
Known2 = KnownBits(BitWidth); |
| 1466 |
|
1466 |
|
| 1467 |
// Recurse, but cap the recursion to one level, because we don't |
1467 |
// Recurse, but cap the recursion to one level, because we don't |
| 1468 |
// want to waste time spinning around in loops. |
1468 |
// want to waste time spinning around in loops. |
| 1469 |
computeKnownBits(IncValue, Known2, MaxAnalysisRecursionDepth - 1, RecQ); |
1469 |
computeKnownBits(IncValue, Known2, MaxAnalysisRecursionDepth - 1, RecQ); |
| 1470 |
|
1470 |
|
| 1471 |
// If this failed, see if we can use a conditional branch into the phi |
1471 |
// If this failed, see if we can use a conditional branch into the phi |
| 1472 |
// to help us determine the range of the value. |
1472 |
// to help us determine the range of the value. |
| 1473 |
if (Known2.isUnknown()) { |
1473 |
if (Known2.isUnknown()) { |
| 1474 |
ICmpInst::Predicate Pred; |
1474 |
ICmpInst::Predicate Pred; |
| 1475 |
const APInt *RHSC; |
1475 |
const APInt *RHSC; |
| 1476 |
BasicBlock *TrueSucc, *FalseSucc; |
1476 |
BasicBlock *TrueSucc, *FalseSucc; |
| 1477 |
// TODO: Use RHS Value and compute range from its known bits. |
1477 |
// TODO: Use RHS Value and compute range from its known bits. |
| 1478 |
if (match(RecQ.CxtI, |
1478 |
if (match(RecQ.CxtI, |
| 1479 |
m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)), |
1479 |
m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)), |
| 1480 |
m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) { |
1480 |
m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) { |
| 1481 |
// Check for cases of duplicate successors. |
1481 |
// Check for cases of duplicate successors. |
| 1482 |
if ((TrueSucc == P->getParent()) != (FalseSucc == P->getParent())) { |
1482 |
if ((TrueSucc == P->getParent()) != (FalseSucc == P->getParent())) { |
| 1483 |
// If we're using the false successor, invert the predicate. |
1483 |
// If we're using the false successor, invert the predicate. |
| 1484 |
if (FalseSucc == P->getParent()) |
1484 |
if (FalseSucc == P->getParent()) |
| 1485 |
Pred = CmpInst::getInversePredicate(Pred); |
1485 |
Pred = CmpInst::getInversePredicate(Pred); |
| 1486 |
|
1486 |
|
| 1487 |
switch (Pred) { |
1487 |
switch (Pred) { |
| 1488 |
case CmpInst::Predicate::ICMP_EQ: |
1488 |
case CmpInst::Predicate::ICMP_EQ: |
| 1489 |
Known2 = KnownBits::makeConstant(*RHSC); |
1489 |
Known2 = KnownBits::makeConstant(*RHSC); |
| 1490 |
break; |
1490 |
break; |
| 1491 |
case CmpInst::Predicate::ICMP_ULE: |
1491 |
case CmpInst::Predicate::ICMP_ULE: |
| 1492 |
Known2.Zero.setHighBits(RHSC->countl_zero()); |
1492 |
Known2.Zero.setHighBits(RHSC->countl_zero()); |
| 1493 |
break; |
1493 |
break; |
| 1494 |
case CmpInst::Predicate::ICMP_ULT: |
1494 |
case CmpInst::Predicate::ICMP_ULT: |
| 1495 |
Known2.Zero.setHighBits((*RHSC - 1).countl_zero()); |
1495 |
Known2.Zero.setHighBits((*RHSC - 1).countl_zero()); |
| 1496 |
break; |
1496 |
break; |
| 1497 |
default: |
1497 |
default: |
| 1498 |
// TODO - add additional integer predicate handling. |
1498 |
// TODO - add additional integer predicate handling. |
| 1499 |
break; |
1499 |
break; |
| 1500 |
} |
1500 |
} |
| 1501 |
} |
1501 |
} |
| 1502 |
} |
1502 |
} |
| 1503 |
} |
1503 |
} |
| 1504 |
|
1504 |
|
| 1505 |
Known = Known.intersectWith(Known2); |
1505 |
Known = Known.intersectWith(Known2); |
| 1506 |
// If all bits have been ruled out, there's no need to check |
1506 |
// If all bits have been ruled out, there's no need to check |
| 1507 |
// more operands. |
1507 |
// more operands. |
| 1508 |
if (Known.isUnknown()) |
1508 |
if (Known.isUnknown()) |
| 1509 |
break; |
1509 |
break; |
| 1510 |
} |
1510 |
} |
| 1511 |
} |
1511 |
} |
| 1512 |
break; |
1512 |
break; |
| 1513 |
} |
1513 |
} |
| 1514 |
case Instruction::Call: |
1514 |
case Instruction::Call: |
| 1515 |
case Instruction::Invoke: |
1515 |
case Instruction::Invoke: |
| 1516 |
// If range metadata is attached to this call, set known bits from that, |
1516 |
// If range metadata is attached to this call, set known bits from that, |
| 1517 |
// and then intersect with known bits based on other properties of the |
1517 |
// and then intersect with known bits based on other properties of the |
| 1518 |
// function. |
1518 |
// function. |
| 1519 |
if (MDNode *MD = |
1519 |
if (MDNode *MD = |
| 1520 |
Q.IIQ.getMetadata(cast(I), LLVMContext::MD_range)) |
1520 |
Q.IIQ.getMetadata(cast(I), LLVMContext::MD_range)) |
| 1521 |
computeKnownBitsFromRangeMetadata(*MD, Known); |
1521 |
computeKnownBitsFromRangeMetadata(*MD, Known); |
| 1522 |
if (const Value *RV = cast(I)->getReturnedArgOperand()) { |
1522 |
if (const Value *RV = cast(I)->getReturnedArgOperand()) { |
| 1523 |
computeKnownBits(RV, Known2, Depth + 1, Q); |
1523 |
computeKnownBits(RV, Known2, Depth + 1, Q); |
| 1524 |
Known = Known.unionWith(Known2); |
1524 |
Known = Known.unionWith(Known2); |
| 1525 |
} |
1525 |
} |
| 1526 |
if (const IntrinsicInst *II = dyn_cast(I)) { |
1526 |
if (const IntrinsicInst *II = dyn_cast(I)) { |
| 1527 |
switch (II->getIntrinsicID()) { |
1527 |
switch (II->getIntrinsicID()) { |
| 1528 |
default: break; |
1528 |
default: break; |
| 1529 |
case Intrinsic::abs: { |
1529 |
case Intrinsic::abs: { |
| 1530 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
1530 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1531 |
bool IntMinIsPoison = match(II->getArgOperand(1), m_One()); |
1531 |
bool IntMinIsPoison = match(II->getArgOperand(1), m_One()); |
| 1532 |
Known = Known2.abs(IntMinIsPoison); |
1532 |
Known = Known2.abs(IntMinIsPoison); |
| 1533 |
break; |
1533 |
break; |
| 1534 |
} |
1534 |
} |
| 1535 |
case Intrinsic::bitreverse: |
1535 |
case Intrinsic::bitreverse: |
| 1536 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
1536 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
| 1537 |
Known.Zero |= Known2.Zero.reverseBits(); |
1537 |
Known.Zero |= Known2.Zero.reverseBits(); |
| 1538 |
Known.One |= Known2.One.reverseBits(); |
1538 |
Known.One |= Known2.One.reverseBits(); |
| 1539 |
break; |
1539 |
break; |
| 1540 |
case Intrinsic::bswap: |
1540 |
case Intrinsic::bswap: |
| 1541 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
1541 |
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q); |
| 1542 |
Known.Zero |= Known2.Zero.byteSwap(); |
1542 |
Known.Zero |= Known2.Zero.byteSwap(); |
| 1543 |
Known.One |= Known2.One.byteSwap(); |
1543 |
Known.One |= Known2.One.byteSwap(); |
| 1544 |
break; |
1544 |
break; |
| 1545 |
case Intrinsic::ctlz: { |
1545 |
case Intrinsic::ctlz: { |
| 1546 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
1546 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1547 |
// If we have a known 1, its position is our upper bound. |
1547 |
// If we have a known 1, its position is our upper bound. |
| 1548 |
unsigned PossibleLZ = Known2.countMaxLeadingZeros(); |
1548 |
unsigned PossibleLZ = Known2.countMaxLeadingZeros(); |
| 1549 |
// If this call is poison for 0 input, the result will be less than 2^n. |
1549 |
// If this call is poison for 0 input, the result will be less than 2^n. |
| 1550 |
if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) |
1550 |
if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) |
| 1551 |
PossibleLZ = std::min(PossibleLZ, BitWidth - 1); |
1551 |
PossibleLZ = std::min(PossibleLZ, BitWidth - 1); |
| 1552 |
unsigned LowBits = llvm::bit_width(PossibleLZ); |
1552 |
unsigned LowBits = llvm::bit_width(PossibleLZ); |
| 1553 |
Known.Zero.setBitsFrom(LowBits); |
1553 |
Known.Zero.setBitsFrom(LowBits); |
| 1554 |
break; |
1554 |
break; |
| 1555 |
} |
1555 |
} |
| 1556 |
case Intrinsic::cttz: { |
1556 |
case Intrinsic::cttz: { |
| 1557 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
1557 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1558 |
// If we have a known 1, its position is our upper bound. |
1558 |
// If we have a known 1, its position is our upper bound. |
| 1559 |
unsigned PossibleTZ = Known2.countMaxTrailingZeros(); |
1559 |
unsigned PossibleTZ = Known2.countMaxTrailingZeros(); |
| 1560 |
// If this call is poison for 0 input, the result will be less than 2^n. |
1560 |
// If this call is poison for 0 input, the result will be less than 2^n. |
| 1561 |
if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) |
1561 |
if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) |
| 1562 |
PossibleTZ = std::min(PossibleTZ, BitWidth - 1); |
1562 |
PossibleTZ = std::min(PossibleTZ, BitWidth - 1); |
| 1563 |
unsigned LowBits = llvm::bit_width(PossibleTZ); |
1563 |
unsigned LowBits = llvm::bit_width(PossibleTZ); |
| 1564 |
Known.Zero.setBitsFrom(LowBits); |
1564 |
Known.Zero.setBitsFrom(LowBits); |
| 1565 |
break; |
1565 |
break; |
| 1566 |
} |
1566 |
} |
| 1567 |
case Intrinsic::ctpop: { |
1567 |
case Intrinsic::ctpop: { |
| 1568 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
1568 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1569 |
// We can bound the space the count needs. Also, bits known to be zero |
1569 |
// We can bound the space the count needs. Also, bits known to be zero |
| 1570 |
// can't contribute to the population. |
1570 |
// can't contribute to the population. |
| 1571 |
unsigned BitsPossiblySet = Known2.countMaxPopulation(); |
1571 |
unsigned BitsPossiblySet = Known2.countMaxPopulation(); |
| 1572 |
unsigned LowBits = llvm::bit_width(BitsPossiblySet); |
1572 |
unsigned LowBits = llvm::bit_width(BitsPossiblySet); |
| 1573 |
Known.Zero.setBitsFrom(LowBits); |
1573 |
Known.Zero.setBitsFrom(LowBits); |
| 1574 |
// TODO: we could bound KnownOne using the lower bound on the number |
1574 |
// TODO: we could bound KnownOne using the lower bound on the number |
| 1575 |
// of bits which might be set provided by popcnt KnownOne2. |
1575 |
// of bits which might be set provided by popcnt KnownOne2. |
| 1576 |
break; |
1576 |
break; |
| 1577 |
} |
1577 |
} |
| 1578 |
case Intrinsic::fshr: |
1578 |
case Intrinsic::fshr: |
| 1579 |
case Intrinsic::fshl: { |
1579 |
case Intrinsic::fshl: { |
| 1580 |
const APInt *SA; |
1580 |
const APInt *SA; |
| 1581 |
if (!match(I->getOperand(2), m_APInt(SA))) |
1581 |
if (!match(I->getOperand(2), m_APInt(SA))) |
| 1582 |
break; |
1582 |
break; |
| 1583 |
|
1583 |
|
| 1584 |
// Normalize to funnel shift left. |
1584 |
// Normalize to funnel shift left. |
| 1585 |
uint64_t ShiftAmt = SA->urem(BitWidth); |
1585 |
uint64_t ShiftAmt = SA->urem(BitWidth); |
| 1586 |
if (II->getIntrinsicID() == Intrinsic::fshr) |
1586 |
if (II->getIntrinsicID() == Intrinsic::fshr) |
| 1587 |
ShiftAmt = BitWidth - ShiftAmt; |
1587 |
ShiftAmt = BitWidth - ShiftAmt; |
| 1588 |
|
1588 |
|
| 1589 |
KnownBits Known3(BitWidth); |
1589 |
KnownBits Known3(BitWidth); |
| 1590 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
1590 |
computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1591 |
computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q); |
1591 |
computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q); |
| 1592 |
|
1592 |
|
| 1593 |
Known.Zero = |
1593 |
Known.Zero = |
| 1594 |
Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt); |
1594 |
Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt); |
| 1595 |
Known.One = |
1595 |
Known.One = |
| 1596 |
Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt); |
1596 |
Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt); |
| 1597 |
break; |
1597 |
break; |
| 1598 |
} |
1598 |
} |
| 1599 |
case Intrinsic::uadd_sat: |
1599 |
case Intrinsic::uadd_sat: |
| 1600 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1600 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1601 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1601 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1602 |
Known = KnownBits::uadd_sat(Known, Known2); |
1602 |
Known = KnownBits::uadd_sat(Known, Known2); |
| 1603 |
break; |
1603 |
break; |
| 1604 |
case Intrinsic::usub_sat: |
1604 |
case Intrinsic::usub_sat: |
| 1605 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1605 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1606 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1606 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1607 |
Known = KnownBits::usub_sat(Known, Known2); |
1607 |
Known = KnownBits::usub_sat(Known, Known2); |
| 1608 |
break; |
1608 |
break; |
| 1609 |
case Intrinsic::sadd_sat: |
1609 |
case Intrinsic::sadd_sat: |
| 1610 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1610 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1611 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1611 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1612 |
Known = KnownBits::sadd_sat(Known, Known2); |
1612 |
Known = KnownBits::sadd_sat(Known, Known2); |
| 1613 |
break; |
1613 |
break; |
| 1614 |
case Intrinsic::ssub_sat: |
1614 |
case Intrinsic::ssub_sat: |
| 1615 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1615 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1616 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1616 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1617 |
Known = KnownBits::ssub_sat(Known, Known2); |
1617 |
Known = KnownBits::ssub_sat(Known, Known2); |
| 1618 |
break; |
1618 |
break; |
| 1619 |
case Intrinsic::umin: |
1619 |
case Intrinsic::umin: |
| 1620 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1620 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1621 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1621 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1622 |
Known = KnownBits::umin(Known, Known2); |
1622 |
Known = KnownBits::umin(Known, Known2); |
| 1623 |
break; |
1623 |
break; |
| 1624 |
case Intrinsic::umax: |
1624 |
case Intrinsic::umax: |
| 1625 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1625 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1626 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1626 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1627 |
Known = KnownBits::umax(Known, Known2); |
1627 |
Known = KnownBits::umax(Known, Known2); |
| 1628 |
break; |
1628 |
break; |
| 1629 |
case Intrinsic::smin: |
1629 |
case Intrinsic::smin: |
| 1630 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1630 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1631 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1631 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1632 |
Known = KnownBits::smin(Known, Known2); |
1632 |
Known = KnownBits::smin(Known, Known2); |
| 1633 |
break; |
1633 |
break; |
| 1634 |
case Intrinsic::smax: |
1634 |
case Intrinsic::smax: |
| 1635 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1635 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1636 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
1636 |
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
| 1637 |
Known = KnownBits::smax(Known, Known2); |
1637 |
Known = KnownBits::smax(Known, Known2); |
| 1638 |
break; |
1638 |
break; |
| 1639 |
case Intrinsic::ptrmask: { |
1639 |
case Intrinsic::ptrmask: { |
| 1640 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1640 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1641 |
|
1641 |
|
| 1642 |
const Value *Mask = I->getOperand(1); |
1642 |
const Value *Mask = I->getOperand(1); |
| 1643 |
Known2 = KnownBits(Mask->getType()->getScalarSizeInBits()); |
1643 |
Known2 = KnownBits(Mask->getType()->getScalarSizeInBits()); |
| 1644 |
computeKnownBits(Mask, Known2, Depth + 1, Q); |
1644 |
computeKnownBits(Mask, Known2, Depth + 1, Q); |
| 1645 |
// This is basically a pointer typed and. |
1645 |
// This is basically a pointer typed and. |
| 1646 |
Known &= Known2.zextOrTrunc(Known.getBitWidth()); |
1646 |
Known &= Known2.zextOrTrunc(Known.getBitWidth()); |
| 1647 |
break; |
1647 |
break; |
| 1648 |
} |
1648 |
} |
| 1649 |
case Intrinsic::x86_sse42_crc32_64_64: |
1649 |
case Intrinsic::x86_sse42_crc32_64_64: |
| 1650 |
Known.Zero.setBitsFrom(32); |
1650 |
Known.Zero.setBitsFrom(32); |
| 1651 |
break; |
1651 |
break; |
| 1652 |
case Intrinsic::riscv_vsetvli: |
1652 |
case Intrinsic::riscv_vsetvli: |
| 1653 |
case Intrinsic::riscv_vsetvlimax: |
1653 |
case Intrinsic::riscv_vsetvlimax: |
| 1654 |
// Assume that VL output is >= 65536. |
1654 |
// Assume that VL output is >= 65536. |
| 1655 |
// TODO: Take SEW and LMUL into account. |
1655 |
// TODO: Take SEW and LMUL into account. |
| 1656 |
if (BitWidth > 17) |
1656 |
if (BitWidth > 17) |
| 1657 |
Known.Zero.setBitsFrom(17); |
1657 |
Known.Zero.setBitsFrom(17); |
| 1658 |
break; |
1658 |
break; |
| 1659 |
case Intrinsic::vscale: { |
1659 |
case Intrinsic::vscale: { |
| 1660 |
if (!II->getParent() || !II->getFunction()) |
1660 |
if (!II->getParent() || !II->getFunction()) |
| 1661 |
break; |
1661 |
break; |
| 1662 |
|
1662 |
|
| 1663 |
Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits(); |
1663 |
Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits(); |
| 1664 |
break; |
1664 |
break; |
| 1665 |
} |
1665 |
} |
| 1666 |
} |
1666 |
} |
| 1667 |
} |
1667 |
} |
| 1668 |
break; |
1668 |
break; |
| 1669 |
case Instruction::ShuffleVector: { |
1669 |
case Instruction::ShuffleVector: { |
| 1670 |
auto *Shuf = dyn_cast(I); |
1670 |
auto *Shuf = dyn_cast(I); |
| 1671 |
// FIXME: Do we need to handle ConstantExpr involving shufflevectors? |
1671 |
// FIXME: Do we need to handle ConstantExpr involving shufflevectors? |
| 1672 |
if (!Shuf) { |
1672 |
if (!Shuf) { |
| 1673 |
Known.resetAll(); |
1673 |
Known.resetAll(); |
| 1674 |
return; |
1674 |
return; |
| 1675 |
} |
1675 |
} |
| 1676 |
// For undef elements, we don't know anything about the common state of |
1676 |
// For undef elements, we don't know anything about the common state of |
| 1677 |
// the shuffle result. |
1677 |
// the shuffle result. |
| 1678 |
APInt DemandedLHS, DemandedRHS; |
1678 |
APInt DemandedLHS, DemandedRHS; |
| 1679 |
if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) { |
1679 |
if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) { |
| 1680 |
Known.resetAll(); |
1680 |
Known.resetAll(); |
| 1681 |
return; |
1681 |
return; |
| 1682 |
} |
1682 |
} |
| 1683 |
Known.One.setAllBits(); |
1683 |
Known.One.setAllBits(); |
| 1684 |
Known.Zero.setAllBits(); |
1684 |
Known.Zero.setAllBits(); |
| 1685 |
if (!!DemandedLHS) { |
1685 |
if (!!DemandedLHS) { |
| 1686 |
const Value *LHS = Shuf->getOperand(0); |
1686 |
const Value *LHS = Shuf->getOperand(0); |
| 1687 |
computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q); |
1687 |
computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q); |
| 1688 |
// If we don't know any bits, early out. |
1688 |
// If we don't know any bits, early out. |
| 1689 |
if (Known.isUnknown()) |
1689 |
if (Known.isUnknown()) |
| 1690 |
break; |
1690 |
break; |
| 1691 |
} |
1691 |
} |
| 1692 |
if (!!DemandedRHS) { |
1692 |
if (!!DemandedRHS) { |
| 1693 |
const Value *RHS = Shuf->getOperand(1); |
1693 |
const Value *RHS = Shuf->getOperand(1); |
| 1694 |
computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q); |
1694 |
computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q); |
| 1695 |
Known = Known.intersectWith(Known2); |
1695 |
Known = Known.intersectWith(Known2); |
| 1696 |
} |
1696 |
} |
| 1697 |
break; |
1697 |
break; |
| 1698 |
} |
1698 |
} |
| 1699 |
case Instruction::InsertElement: { |
1699 |
case Instruction::InsertElement: { |
| 1700 |
if (isa(I->getType())) { |
1700 |
if (isa(I->getType())) { |
| 1701 |
Known.resetAll(); |
1701 |
Known.resetAll(); |
| 1702 |
return; |
1702 |
return; |
| 1703 |
} |
1703 |
} |
| 1704 |
const Value *Vec = I->getOperand(0); |
1704 |
const Value *Vec = I->getOperand(0); |
| 1705 |
const Value *Elt = I->getOperand(1); |
1705 |
const Value *Elt = I->getOperand(1); |
| 1706 |
auto *CIdx = dyn_cast(I->getOperand(2)); |
1706 |
auto *CIdx = dyn_cast(I->getOperand(2)); |
| 1707 |
// Early out if the index is non-constant or out-of-range. |
1707 |
// Early out if the index is non-constant or out-of-range. |
| 1708 |
unsigned NumElts = DemandedElts.getBitWidth(); |
1708 |
unsigned NumElts = DemandedElts.getBitWidth(); |
| 1709 |
if (!CIdx || CIdx->getValue().uge(NumElts)) { |
1709 |
if (!CIdx || CIdx->getValue().uge(NumElts)) { |
| 1710 |
Known.resetAll(); |
1710 |
Known.resetAll(); |
| 1711 |
return; |
1711 |
return; |
| 1712 |
} |
1712 |
} |
| 1713 |
Known.One.setAllBits(); |
1713 |
Known.One.setAllBits(); |
| 1714 |
Known.Zero.setAllBits(); |
1714 |
Known.Zero.setAllBits(); |
| 1715 |
unsigned EltIdx = CIdx->getZExtValue(); |
1715 |
unsigned EltIdx = CIdx->getZExtValue(); |
| 1716 |
// Do we demand the inserted element? |
1716 |
// Do we demand the inserted element? |
| 1717 |
if (DemandedElts[EltIdx]) { |
1717 |
if (DemandedElts[EltIdx]) { |
| 1718 |
computeKnownBits(Elt, Known, Depth + 1, Q); |
1718 |
computeKnownBits(Elt, Known, Depth + 1, Q); |
| 1719 |
// If we don't know any bits, early out. |
1719 |
// If we don't know any bits, early out. |
| 1720 |
if (Known.isUnknown()) |
1720 |
if (Known.isUnknown()) |
| 1721 |
break; |
1721 |
break; |
| 1722 |
} |
1722 |
} |
| 1723 |
// We don't need the base vector element that has been inserted. |
1723 |
// We don't need the base vector element that has been inserted. |
| 1724 |
APInt DemandedVecElts = DemandedElts; |
1724 |
APInt DemandedVecElts = DemandedElts; |
| 1725 |
DemandedVecElts.clearBit(EltIdx); |
1725 |
DemandedVecElts.clearBit(EltIdx); |
| 1726 |
if (!!DemandedVecElts) { |
1726 |
if (!!DemandedVecElts) { |
| 1727 |
computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q); |
1727 |
computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q); |
| 1728 |
Known = Known.intersectWith(Known2); |
1728 |
Known = Known.intersectWith(Known2); |
| 1729 |
} |
1729 |
} |
| 1730 |
break; |
1730 |
break; |
| 1731 |
} |
1731 |
} |
| 1732 |
case Instruction::ExtractElement: { |
1732 |
case Instruction::ExtractElement: { |
| 1733 |
// Look through extract element. If the index is non-constant or |
1733 |
// Look through extract element. If the index is non-constant or |
| 1734 |
// out-of-range demand all elements, otherwise just the extracted element. |
1734 |
// out-of-range demand all elements, otherwise just the extracted element. |
| 1735 |
const Value *Vec = I->getOperand(0); |
1735 |
const Value *Vec = I->getOperand(0); |
| 1736 |
const Value *Idx = I->getOperand(1); |
1736 |
const Value *Idx = I->getOperand(1); |
| 1737 |
auto *CIdx = dyn_cast(Idx); |
1737 |
auto *CIdx = dyn_cast(Idx); |
| 1738 |
if (isa(Vec->getType())) { |
1738 |
if (isa(Vec->getType())) { |
| 1739 |
// FIXME: there's probably *something* we can do with scalable vectors |
1739 |
// FIXME: there's probably *something* we can do with scalable vectors |
| 1740 |
Known.resetAll(); |
1740 |
Known.resetAll(); |
| 1741 |
break; |
1741 |
break; |
| 1742 |
} |
1742 |
} |
| 1743 |
unsigned NumElts = cast(Vec->getType())->getNumElements(); |
1743 |
unsigned NumElts = cast(Vec->getType())->getNumElements(); |
| 1744 |
APInt DemandedVecElts = APInt::getAllOnes(NumElts); |
1744 |
APInt DemandedVecElts = APInt::getAllOnes(NumElts); |
| 1745 |
if (CIdx && CIdx->getValue().ult(NumElts)) |
1745 |
if (CIdx && CIdx->getValue().ult(NumElts)) |
| 1746 |
DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue()); |
1746 |
DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue()); |
| 1747 |
computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q); |
1747 |
computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q); |
| 1748 |
break; |
1748 |
break; |
| 1749 |
} |
1749 |
} |
| 1750 |
case Instruction::ExtractValue: |
1750 |
case Instruction::ExtractValue: |
| 1751 |
if (IntrinsicInst *II = dyn_cast(I->getOperand(0))) { |
1751 |
if (IntrinsicInst *II = dyn_cast(I->getOperand(0))) { |
| 1752 |
const ExtractValueInst *EVI = cast(I); |
1752 |
const ExtractValueInst *EVI = cast(I); |
| 1753 |
if (EVI->getNumIndices() != 1) break; |
1753 |
if (EVI->getNumIndices() != 1) break; |
| 1754 |
if (EVI->getIndices()[0] == 0) { |
1754 |
if (EVI->getIndices()[0] == 0) { |
| 1755 |
switch (II->getIntrinsicID()) { |
1755 |
switch (II->getIntrinsicID()) { |
| 1756 |
default: break; |
1756 |
default: break; |
| 1757 |
case Intrinsic::uadd_with_overflow: |
1757 |
case Intrinsic::uadd_with_overflow: |
| 1758 |
case Intrinsic::sadd_with_overflow: |
1758 |
case Intrinsic::sadd_with_overflow: |
| 1759 |
computeKnownBitsAddSub(true, II->getArgOperand(0), |
1759 |
computeKnownBitsAddSub(true, II->getArgOperand(0), |
| 1760 |
II->getArgOperand(1), false, DemandedElts, |
1760 |
II->getArgOperand(1), false, DemandedElts, |
| 1761 |
Known, Known2, Depth, Q); |
1761 |
Known, Known2, Depth, Q); |
| 1762 |
break; |
1762 |
break; |
| 1763 |
case Intrinsic::usub_with_overflow: |
1763 |
case Intrinsic::usub_with_overflow: |
| 1764 |
case Intrinsic::ssub_with_overflow: |
1764 |
case Intrinsic::ssub_with_overflow: |
| 1765 |
computeKnownBitsAddSub(false, II->getArgOperand(0), |
1765 |
computeKnownBitsAddSub(false, II->getArgOperand(0), |
| 1766 |
II->getArgOperand(1), false, DemandedElts, |
1766 |
II->getArgOperand(1), false, DemandedElts, |
| 1767 |
Known, Known2, Depth, Q); |
1767 |
Known, Known2, Depth, Q); |
| 1768 |
break; |
1768 |
break; |
| 1769 |
case Intrinsic::umul_with_overflow: |
1769 |
case Intrinsic::umul_with_overflow: |
| 1770 |
case Intrinsic::smul_with_overflow: |
1770 |
case Intrinsic::smul_with_overflow: |
| 1771 |
computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false, |
1771 |
computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false, |
| 1772 |
DemandedElts, Known, Known2, Depth, Q); |
1772 |
DemandedElts, Known, Known2, Depth, Q); |
| 1773 |
break; |
1773 |
break; |
| 1774 |
} |
1774 |
} |
| 1775 |
} |
1775 |
} |
| 1776 |
} |
1776 |
} |
| 1777 |
break; |
1777 |
break; |
| 1778 |
case Instruction::Freeze: |
1778 |
case Instruction::Freeze: |
| 1779 |
if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT, |
1779 |
if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT, |
| 1780 |
Depth + 1)) |
1780 |
Depth + 1)) |
| 1781 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
1781 |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1782 |
break; |
1782 |
break; |
| 1783 |
} |
1783 |
} |
| 1784 |
} |
1784 |
} |
| 1785 |
|
1785 |
|
| 1786 |
/// Determine which bits of V are known to be either zero or one and return |
1786 |
/// Determine which bits of V are known to be either zero or one and return |
| 1787 |
/// them. |
1787 |
/// them. |
| 1788 |
KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, |
1788 |
KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, |
| 1789 |
unsigned Depth, const SimplifyQuery &Q) { |
1789 |
unsigned Depth, const SimplifyQuery &Q) { |
| 1790 |
KnownBits Known(getBitWidth(V->getType(), Q.DL)); |
1790 |
KnownBits Known(getBitWidth(V->getType(), Q.DL)); |
| 1791 |
computeKnownBits(V, DemandedElts, Known, Depth, Q); |
1791 |
computeKnownBits(V, DemandedElts, Known, Depth, Q); |
| 1792 |
return Known; |
1792 |
return Known; |
| 1793 |
} |
1793 |
} |
| 1794 |
|
1794 |
|
| 1795 |
/// Determine which bits of V are known to be either zero or one and return |
1795 |
/// Determine which bits of V are known to be either zero or one and return |
| 1796 |
/// them. |
1796 |
/// them. |
| 1797 |
KnownBits computeKnownBits(const Value *V, unsigned Depth, |
1797 |
KnownBits computeKnownBits(const Value *V, unsigned Depth, |
| 1798 |
const SimplifyQuery &Q) { |
1798 |
const SimplifyQuery &Q) { |
| 1799 |
KnownBits Known(getBitWidth(V->getType(), Q.DL)); |
1799 |
KnownBits Known(getBitWidth(V->getType(), Q.DL)); |
| 1800 |
computeKnownBits(V, Known, Depth, Q); |
1800 |
computeKnownBits(V, Known, Depth, Q); |
| 1801 |
return Known; |
1801 |
return Known; |
| 1802 |
} |
1802 |
} |
| 1803 |
|
1803 |
|
| 1804 |
/// Determine which bits of V are known to be either zero or one and return |
1804 |
/// Determine which bits of V are known to be either zero or one and return |
| 1805 |
/// them in the Known bit set. |
1805 |
/// them in the Known bit set. |
| 1806 |
/// |
1806 |
/// |
| 1807 |
/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
1807 |
/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 1808 |
/// we cannot optimize based on the assumption that it is zero without changing |
1808 |
/// we cannot optimize based on the assumption that it is zero without changing |
| 1809 |
/// it to be an explicit zero. If we don't change it to zero, other code could |
1809 |
/// it to be an explicit zero. If we don't change it to zero, other code could |
| 1810 |
/// optimized based on the contradictory assumption that it is non-zero. |
1810 |
/// optimized based on the contradictory assumption that it is non-zero. |
| 1811 |
/// Because instcombine aggressively folds operations with undef args anyway, |
1811 |
/// Because instcombine aggressively folds operations with undef args anyway, |
| 1812 |
/// this won't lose us code quality. |
1812 |
/// this won't lose us code quality. |
| 1813 |
/// |
1813 |
/// |
| 1814 |
/// This function is defined on values with integer type, values with pointer |
1814 |
/// This function is defined on values with integer type, values with pointer |
| 1815 |
/// type, and vectors of integers. In the case |
1815 |
/// type, and vectors of integers. In the case |
| 1816 |
/// where V is a vector, known zero, and known one values are the |
1816 |
/// where V is a vector, known zero, and known one values are the |
| 1817 |
/// same width as the vector element, and the bit is set only if it is true |
1817 |
/// same width as the vector element, and the bit is set only if it is true |
| 1818 |
/// for all of the demanded elements in the vector specified by DemandedElts. |
1818 |
/// for all of the demanded elements in the vector specified by DemandedElts. |
| 1819 |
void computeKnownBits(const Value *V, const APInt &DemandedElts, |
1819 |
void computeKnownBits(const Value *V, const APInt &DemandedElts, |
| 1820 |
KnownBits &Known, unsigned Depth, |
1820 |
KnownBits &Known, unsigned Depth, |
| 1821 |
const SimplifyQuery &Q) { |
1821 |
const SimplifyQuery &Q) { |
| 1822 |
if (!DemandedElts) { |
1822 |
if (!DemandedElts) { |
| 1823 |
// No demanded elts, better to assume we don't know anything. |
1823 |
// No demanded elts, better to assume we don't know anything. |
| 1824 |
Known.resetAll(); |
1824 |
Known.resetAll(); |
| 1825 |
return; |
1825 |
return; |
| 1826 |
} |
1826 |
} |
| 1827 |
|
1827 |
|
| 1828 |
assert(V && "No Value?"); |
1828 |
assert(V && "No Value?"); |
| 1829 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
1829 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
| 1830 |
|
1830 |
|
| 1831 |
#ifndef NDEBUG |
1831 |
#ifndef NDEBUG |
| 1832 |
Type *Ty = V->getType(); |
1832 |
Type *Ty = V->getType(); |
| 1833 |
unsigned BitWidth = Known.getBitWidth(); |
1833 |
unsigned BitWidth = Known.getBitWidth(); |
| 1834 |
|
1834 |
|
| 1835 |
assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) && |
1835 |
assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) && |
| 1836 |
"Not integer or pointer type!"); |
1836 |
"Not integer or pointer type!"); |
| 1837 |
|
1837 |
|
| 1838 |
if (auto *FVTy = dyn_cast(Ty)) { |
1838 |
if (auto *FVTy = dyn_cast(Ty)) { |
| 1839 |
assert( |
1839 |
assert( |
| 1840 |
FVTy->getNumElements() == DemandedElts.getBitWidth() && |
1840 |
FVTy->getNumElements() == DemandedElts.getBitWidth() && |
| 1841 |
"DemandedElt width should equal the fixed vector number of elements"); |
1841 |
"DemandedElt width should equal the fixed vector number of elements"); |
| 1842 |
} else { |
1842 |
} else { |
| 1843 |
assert(DemandedElts == APInt(1, 1) && |
1843 |
assert(DemandedElts == APInt(1, 1) && |
| 1844 |
"DemandedElt width should be 1 for scalars or scalable vectors"); |
1844 |
"DemandedElt width should be 1 for scalars or scalable vectors"); |
| 1845 |
} |
1845 |
} |
| 1846 |
|
1846 |
|
| 1847 |
Type *ScalarTy = Ty->getScalarType(); |
1847 |
Type *ScalarTy = Ty->getScalarType(); |
| 1848 |
if (ScalarTy->isPointerTy()) { |
1848 |
if (ScalarTy->isPointerTy()) { |
| 1849 |
assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) && |
1849 |
assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) && |
| 1850 |
"V and Known should have same BitWidth"); |
1850 |
"V and Known should have same BitWidth"); |
| 1851 |
} else { |
1851 |
} else { |
| 1852 |
assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) && |
1852 |
assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) && |
| 1853 |
"V and Known should have same BitWidth"); |
1853 |
"V and Known should have same BitWidth"); |
| 1854 |
} |
1854 |
} |
| 1855 |
#endif |
1855 |
#endif |
| 1856 |
|
1856 |
|
| 1857 |
const APInt *C; |
1857 |
const APInt *C; |
| 1858 |
if (match(V, m_APInt(C))) { |
1858 |
if (match(V, m_APInt(C))) { |
| 1859 |
// We know all of the bits for a scalar constant or a splat vector constant! |
1859 |
// We know all of the bits for a scalar constant or a splat vector constant! |
| 1860 |
Known = KnownBits::makeConstant(*C); |
1860 |
Known = KnownBits::makeConstant(*C); |
| 1861 |
return; |
1861 |
return; |
| 1862 |
} |
1862 |
} |
| 1863 |
// Null and aggregate-zero are all-zeros. |
1863 |
// Null and aggregate-zero are all-zeros. |
| 1864 |
if (isa(V) || isa(V)) { |
1864 |
if (isa(V) || isa(V)) { |
| 1865 |
Known.setAllZero(); |
1865 |
Known.setAllZero(); |
| 1866 |
return; |
1866 |
return; |
| 1867 |
} |
1867 |
} |
| 1868 |
// Handle a constant vector by taking the intersection of the known bits of |
1868 |
// Handle a constant vector by taking the intersection of the known bits of |
| 1869 |
// each element. |
1869 |
// each element. |
| 1870 |
if (const ConstantDataVector *CDV = dyn_cast(V)) { |
1870 |
if (const ConstantDataVector *CDV = dyn_cast(V)) { |
| 1871 |
assert(!isa(V->getType())); |
1871 |
assert(!isa(V->getType())); |
| 1872 |
// We know that CDV must be a vector of integers. Take the intersection of |
1872 |
// We know that CDV must be a vector of integers. Take the intersection of |
| 1873 |
// each element. |
1873 |
// each element. |
| 1874 |
Known.Zero.setAllBits(); Known.One.setAllBits(); |
1874 |
Known.Zero.setAllBits(); Known.One.setAllBits(); |
| 1875 |
for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) { |
1875 |
for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) { |
| 1876 |
if (!DemandedElts[i]) |
1876 |
if (!DemandedElts[i]) |
| 1877 |
continue; |
1877 |
continue; |
| 1878 |
APInt Elt = CDV->getElementAsAPInt(i); |
1878 |
APInt Elt = CDV->getElementAsAPInt(i); |
| 1879 |
Known.Zero &= ~Elt; |
1879 |
Known.Zero &= ~Elt; |
| 1880 |
Known.One &= Elt; |
1880 |
Known.One &= Elt; |
| 1881 |
} |
1881 |
} |
| 1882 |
return; |
1882 |
return; |
| 1883 |
} |
1883 |
} |
| 1884 |
|
1884 |
|
| 1885 |
if (const auto *CV = dyn_cast(V)) { |
1885 |
if (const auto *CV = dyn_cast(V)) { |
| 1886 |
assert(!isa(V->getType())); |
1886 |
assert(!isa(V->getType())); |
| 1887 |
// We know that CV must be a vector of integers. Take the intersection of |
1887 |
// We know that CV must be a vector of integers. Take the intersection of |
| 1888 |
// each element. |
1888 |
// each element. |
| 1889 |
Known.Zero.setAllBits(); Known.One.setAllBits(); |
1889 |
Known.Zero.setAllBits(); Known.One.setAllBits(); |
| 1890 |
for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) { |
1890 |
for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) { |
| 1891 |
if (!DemandedElts[i]) |
1891 |
if (!DemandedElts[i]) |
| 1892 |
continue; |
1892 |
continue; |
| 1893 |
Constant *Element = CV->getAggregateElement(i); |
1893 |
Constant *Element = CV->getAggregateElement(i); |
| 1894 |
auto *ElementCI = dyn_cast_or_null(Element); |
1894 |
auto *ElementCI = dyn_cast_or_null(Element); |
| 1895 |
if (!ElementCI) { |
1895 |
if (!ElementCI) { |
| 1896 |
Known.resetAll(); |
1896 |
Known.resetAll(); |
| 1897 |
return; |
1897 |
return; |
| 1898 |
} |
1898 |
} |
| 1899 |
const APInt &Elt = ElementCI->getValue(); |
1899 |
const APInt &Elt = ElementCI->getValue(); |
| 1900 |
Known.Zero &= ~Elt; |
1900 |
Known.Zero &= ~Elt; |
| 1901 |
Known.One &= Elt; |
1901 |
Known.One &= Elt; |
| 1902 |
} |
1902 |
} |
| 1903 |
return; |
1903 |
return; |
| 1904 |
} |
1904 |
} |
| 1905 |
|
1905 |
|
| 1906 |
// Start out not knowing anything. |
1906 |
// Start out not knowing anything. |
| 1907 |
Known.resetAll(); |
1907 |
Known.resetAll(); |
| 1908 |
|
1908 |
|
| 1909 |
// We can't imply anything about undefs. |
1909 |
// We can't imply anything about undefs. |
| 1910 |
if (isa(V)) |
1910 |
if (isa(V)) |
| 1911 |
return; |
1911 |
return; |
| 1912 |
|
1912 |
|
| 1913 |
// There's no point in looking through other users of ConstantData for |
1913 |
// There's no point in looking through other users of ConstantData for |
| 1914 |
// assumptions. Confirm that we've handled them all. |
1914 |
// assumptions. Confirm that we've handled them all. |
| 1915 |
assert(!isa(V) && "Unhandled constant data!"); |
1915 |
assert(!isa(V) && "Unhandled constant data!"); |
| 1916 |
|
1916 |
|
| 1917 |
// All recursive calls that increase depth must come after this. |
1917 |
// All recursive calls that increase depth must come after this. |
| 1918 |
if (Depth == MaxAnalysisRecursionDepth) |
1918 |
if (Depth == MaxAnalysisRecursionDepth) |
| 1919 |
return; |
1919 |
return; |
| 1920 |
|
1920 |
|
| 1921 |
// A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has |
1921 |
// A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has |
| 1922 |
// the bits of its aliasee. |
1922 |
// the bits of its aliasee. |
| 1923 |
if (const GlobalAlias *GA = dyn_cast(V)) { |
1923 |
if (const GlobalAlias *GA = dyn_cast(V)) { |
| 1924 |
if (!GA->isInterposable()) |
1924 |
if (!GA->isInterposable()) |
| 1925 |
computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q); |
1925 |
computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q); |
| 1926 |
return; |
1926 |
return; |
| 1927 |
} |
1927 |
} |
| 1928 |
|
1928 |
|
| 1929 |
if (const Operator *I = dyn_cast(V)) |
1929 |
if (const Operator *I = dyn_cast(V)) |
| 1930 |
computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q); |
1930 |
computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q); |
| 1931 |
else if (const GlobalValue *GV = dyn_cast(V)) { |
1931 |
else if (const GlobalValue *GV = dyn_cast(V)) { |
| 1932 |
if (std::optional CR = GV->getAbsoluteSymbolRange()) |
1932 |
if (std::optional CR = GV->getAbsoluteSymbolRange()) |
| 1933 |
Known = CR->toKnownBits(); |
1933 |
Known = CR->toKnownBits(); |
| 1934 |
} |
1934 |
} |
| 1935 |
|
1935 |
|
| 1936 |
// Aligned pointers have trailing zeros - refine Known.Zero set |
1936 |
// Aligned pointers have trailing zeros - refine Known.Zero set |
| 1937 |
if (isa(V->getType())) { |
1937 |
if (isa(V->getType())) { |
| 1938 |
Align Alignment = V->getPointerAlignment(Q.DL); |
1938 |
Align Alignment = V->getPointerAlignment(Q.DL); |
| 1939 |
Known.Zero.setLowBits(Log2(Alignment)); |
1939 |
Known.Zero.setLowBits(Log2(Alignment)); |
| 1940 |
} |
1940 |
} |
| 1941 |
|
1941 |
|
| 1942 |
// computeKnownBitsFromAssume strictly refines Known. |
1942 |
// computeKnownBitsFromAssume strictly refines Known. |
| 1943 |
// Therefore, we run them after computeKnownBitsFromOperator. |
1943 |
// Therefore, we run them after computeKnownBitsFromOperator. |
| 1944 |
|
1944 |
|
| 1945 |
// Check whether a nearby assume intrinsic can determine some known bits. |
1945 |
// Check whether a nearby assume intrinsic can determine some known bits. |
| 1946 |
computeKnownBitsFromAssume(V, Known, Depth, Q); |
1946 |
computeKnownBitsFromAssume(V, Known, Depth, Q); |
| 1947 |
|
1947 |
|
| 1948 |
assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?"); |
1948 |
assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?"); |
| 1949 |
} |
1949 |
} |
| 1950 |
|
1950 |
|
| 1951 |
/// Try to detect a recurrence that the value of the induction variable is |
1951 |
/// Try to detect a recurrence that the value of the induction variable is |
| 1952 |
/// always a power of two (or zero). |
1952 |
/// always a power of two (or zero). |
| 1953 |
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, |
1953 |
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, |
| 1954 |
unsigned Depth, SimplifyQuery &Q) { |
1954 |
unsigned Depth, SimplifyQuery &Q) { |
| 1955 |
BinaryOperator *BO = nullptr; |
1955 |
BinaryOperator *BO = nullptr; |
| 1956 |
Value *Start = nullptr, *Step = nullptr; |
1956 |
Value *Start = nullptr, *Step = nullptr; |
| 1957 |
if (!matchSimpleRecurrence(PN, BO, Start, Step)) |
1957 |
if (!matchSimpleRecurrence(PN, BO, Start, Step)) |
| 1958 |
return false; |
1958 |
return false; |
| 1959 |
|
1959 |
|
| 1960 |
// Initial value must be a power of two. |
1960 |
// Initial value must be a power of two. |
| 1961 |
for (const Use &U : PN->operands()) { |
1961 |
for (const Use &U : PN->operands()) { |
| 1962 |
if (U.get() == Start) { |
1962 |
if (U.get() == Start) { |
| 1963 |
// Initial value comes from a different BB, need to adjust context |
1963 |
// Initial value comes from a different BB, need to adjust context |
| 1964 |
// instruction for analysis. |
1964 |
// instruction for analysis. |
| 1965 |
Q.CxtI = PN->getIncomingBlock(U)->getTerminator(); |
1965 |
Q.CxtI = PN->getIncomingBlock(U)->getTerminator(); |
| 1966 |
if (!isKnownToBeAPowerOfTwo(Start, OrZero, Depth, Q)) |
1966 |
if (!isKnownToBeAPowerOfTwo(Start, OrZero, Depth, Q)) |
| 1967 |
return false; |
1967 |
return false; |
| 1968 |
} |
1968 |
} |
| 1969 |
} |
1969 |
} |
| 1970 |
|
1970 |
|
| 1971 |
// Except for Mul, the induction variable must be on the left side of the |
1971 |
// Except for Mul, the induction variable must be on the left side of the |
| 1972 |
// increment expression, otherwise its value can be arbitrary. |
1972 |
// increment expression, otherwise its value can be arbitrary. |
| 1973 |
if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step) |
1973 |
if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step) |
| 1974 |
return false; |
1974 |
return false; |
| 1975 |
|
1975 |
|
| 1976 |
Q.CxtI = BO->getParent()->getTerminator(); |
1976 |
Q.CxtI = BO->getParent()->getTerminator(); |
| 1977 |
switch (BO->getOpcode()) { |
1977 |
switch (BO->getOpcode()) { |
| 1978 |
case Instruction::Mul: |
1978 |
case Instruction::Mul: |
| 1979 |
// Power of two is closed under multiplication. |
1979 |
// Power of two is closed under multiplication. |
| 1980 |
return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || |
1980 |
return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || |
| 1981 |
Q.IIQ.hasNoSignedWrap(BO)) && |
1981 |
Q.IIQ.hasNoSignedWrap(BO)) && |
| 1982 |
isKnownToBeAPowerOfTwo(Step, OrZero, Depth, Q); |
1982 |
isKnownToBeAPowerOfTwo(Step, OrZero, Depth, Q); |
| 1983 |
case Instruction::SDiv: |
1983 |
case Instruction::SDiv: |
| 1984 |
// Start value must not be signmask for signed division, so simply being a |
1984 |
// Start value must not be signmask for signed division, so simply being a |
| 1985 |
// power of two is not sufficient, and it has to be a constant. |
1985 |
// power of two is not sufficient, and it has to be a constant. |
| 1986 |
if (!match(Start, m_Power2()) || match(Start, m_SignMask())) |
1986 |
if (!match(Start, m_Power2()) || match(Start, m_SignMask())) |
| 1987 |
return false; |
1987 |
return false; |
| 1988 |
[[fallthrough]]; |
1988 |
[[fallthrough]]; |
| 1989 |
case Instruction::UDiv: |
1989 |
case Instruction::UDiv: |
| 1990 |
// Divisor must be a power of two. |
1990 |
// Divisor must be a power of two. |
| 1991 |
// If OrZero is false, cannot guarantee induction variable is non-zero after |
1991 |
// If OrZero is false, cannot guarantee induction variable is non-zero after |
| 1992 |
// division, same for Shr, unless it is exact division. |
1992 |
// division, same for Shr, unless it is exact division. |
| 1993 |
return (OrZero || Q.IIQ.isExact(BO)) && |
1993 |
return (OrZero || Q.IIQ.isExact(BO)) && |
| 1994 |
isKnownToBeAPowerOfTwo(Step, false, Depth, Q); |
1994 |
isKnownToBeAPowerOfTwo(Step, false, Depth, Q); |
| 1995 |
case Instruction::Shl: |
1995 |
case Instruction::Shl: |
| 1996 |
return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO); |
1996 |
return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO); |
| 1997 |
case Instruction::AShr: |
1997 |
case Instruction::AShr: |
| 1998 |
if (!match(Start, m_Power2()) || match(Start, m_SignMask())) |
1998 |
if (!match(Start, m_Power2()) || match(Start, m_SignMask())) |
| 1999 |
return false; |
1999 |
return false; |
| 2000 |
[[fallthrough]]; |
2000 |
[[fallthrough]]; |
| 2001 |
case Instruction::LShr: |
2001 |
case Instruction::LShr: |
| 2002 |
return OrZero || Q.IIQ.isExact(BO); |
2002 |
return OrZero || Q.IIQ.isExact(BO); |
| 2003 |
default: |
2003 |
default: |
| 2004 |
return false; |
2004 |
return false; |
| 2005 |
} |
2005 |
} |
| 2006 |
} |
2006 |
} |
| 2007 |
|
2007 |
|
| 2008 |
/// Return true if the given value is known to have exactly one |
2008 |
/// Return true if the given value is known to have exactly one |
| 2009 |
/// bit set when defined. For vectors return true if every element is known to |
2009 |
/// bit set when defined. For vectors return true if every element is known to |
| 2010 |
/// be a power of two when defined. Supports values with integer or pointer |
2010 |
/// be a power of two when defined. Supports values with integer or pointer |
| 2011 |
/// types and vectors of integers. |
2011 |
/// types and vectors of integers. |
| 2012 |
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, |
2012 |
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, |
| 2013 |
const SimplifyQuery &Q) { |
2013 |
const SimplifyQuery &Q) { |
| 2014 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
2014 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
| 2015 |
|
2015 |
|
| 2016 |
// Attempt to match against constants. |
2016 |
// Attempt to match against constants. |
| 2017 |
if (OrZero && match(V, m_Power2OrZero())) |
2017 |
if (OrZero && match(V, m_Power2OrZero())) |
| 2018 |
return true; |
2018 |
return true; |
| 2019 |
if (match(V, m_Power2())) |
2019 |
if (match(V, m_Power2())) |
| 2020 |
return true; |
2020 |
return true; |
| 2021 |
if (Q.CxtI && match(V, m_VScale())) { |
2021 |
if (Q.CxtI && match(V, m_VScale())) { |
| 2022 |
const Function *F = Q.CxtI->getFunction(); |
2022 |
const Function *F = Q.CxtI->getFunction(); |
| 2023 |
// The vscale_range indicates vscale is a power-of-two. |
2023 |
// The vscale_range indicates vscale is a power-of-two. |
| 2024 |
return F->hasFnAttribute(Attribute::VScaleRange); |
2024 |
return F->hasFnAttribute(Attribute::VScaleRange); |
| 2025 |
} |
2025 |
} |
| 2026 |
|
2026 |
|
| 2027 |
// 1 << X is clearly a power of two if the one is not shifted off the end. If |
2027 |
// 1 << X is clearly a power of two if the one is not shifted off the end. If |
| 2028 |
// it is shifted off the end then the result is undefined. |
2028 |
// it is shifted off the end then the result is undefined. |
| 2029 |
if (match(V, m_Shl(m_One(), m_Value()))) |
2029 |
if (match(V, m_Shl(m_One(), m_Value()))) |
| 2030 |
return true; |
2030 |
return true; |
| 2031 |
|
2031 |
|
| 2032 |
// (signmask) >>l X is clearly a power of two if the one is not shifted off |
2032 |
// (signmask) >>l X is clearly a power of two if the one is not shifted off |
| 2033 |
// the bottom. If it is shifted off the bottom then the result is undefined. |
2033 |
// the bottom. If it is shifted off the bottom then the result is undefined. |
| 2034 |
if (match(V, m_LShr(m_SignMask(), m_Value()))) |
2034 |
if (match(V, m_LShr(m_SignMask(), m_Value()))) |
| 2035 |
return true; |
2035 |
return true; |
| 2036 |
|
2036 |
|
| 2037 |
// The remaining tests are all recursive, so bail out if we hit the limit. |
2037 |
// The remaining tests are all recursive, so bail out if we hit the limit. |
| 2038 |
if (Depth++ == MaxAnalysisRecursionDepth) |
2038 |
if (Depth++ == MaxAnalysisRecursionDepth) |
| 2039 |
return false; |
2039 |
return false; |
| 2040 |
|
2040 |
|
| 2041 |
Value *X = nullptr, *Y = nullptr; |
2041 |
Value *X = nullptr, *Y = nullptr; |
| 2042 |
// A shift left or a logical shift right of a power of two is a power of two |
2042 |
// A shift left or a logical shift right of a power of two is a power of two |
| 2043 |
// or zero. |
2043 |
// or zero. |
| 2044 |
if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) || |
2044 |
if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) || |
| 2045 |
match(V, m_LShr(m_Value(X), m_Value())))) |
2045 |
match(V, m_LShr(m_Value(X), m_Value())))) |
| 2046 |
return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q); |
2046 |
return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q); |
| 2047 |
|
2047 |
|
| 2048 |
if (const ZExtInst *ZI = dyn_cast(V)) |
2048 |
if (const ZExtInst *ZI = dyn_cast(V)) |
| 2049 |
return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q); |
2049 |
return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q); |
| 2050 |
|
2050 |
|
| 2051 |
if (const SelectInst *SI = dyn_cast(V)) |
2051 |
if (const SelectInst *SI = dyn_cast(V)) |
| 2052 |
return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) && |
2052 |
return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) && |
| 2053 |
isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q); |
2053 |
isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q); |
| 2054 |
|
2054 |
|
| 2055 |
// Peek through min/max. |
2055 |
// Peek through min/max. |
| 2056 |
if (match(V, m_MaxOrMin(m_Value(X), m_Value(Y)))) { |
2056 |
if (match(V, m_MaxOrMin(m_Value(X), m_Value(Y)))) { |
| 2057 |
return isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q) && |
2057 |
return isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q) && |
| 2058 |
isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q); |
2058 |
isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q); |
| 2059 |
} |
2059 |
} |
| 2060 |
|
2060 |
|
| 2061 |
if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) { |
2061 |
if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) { |
| 2062 |
// A power of two and'd with anything is a power of two or zero. |
2062 |
// A power of two and'd with anything is a power of two or zero. |
| 2063 |
if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) || |
2063 |
if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) || |
| 2064 |
isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q)) |
2064 |
isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q)) |
| 2065 |
return true; |
2065 |
return true; |
| 2066 |
// X & (-X) is always a power of two or zero. |
2066 |
// X & (-X) is always a power of two or zero. |
| 2067 |
if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X)))) |
2067 |
if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X)))) |
| 2068 |
return true; |
2068 |
return true; |
| 2069 |
return false; |
2069 |
return false; |
| 2070 |
} |
2070 |
} |
| 2071 |
|
2071 |
|
| 2072 |
// Adding a power-of-two or zero to the same power-of-two or zero yields |
2072 |
// Adding a power-of-two or zero to the same power-of-two or zero yields |
| 2073 |
// either the original power-of-two, a larger power-of-two or zero. |
2073 |
// either the original power-of-two, a larger power-of-two or zero. |
| 2074 |
if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
2074 |
if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
| 2075 |
const OverflowingBinaryOperator *VOBO = cast(V); |
2075 |
const OverflowingBinaryOperator *VOBO = cast(V); |
| 2076 |
if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) || |
2076 |
if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) || |
| 2077 |
Q.IIQ.hasNoSignedWrap(VOBO)) { |
2077 |
Q.IIQ.hasNoSignedWrap(VOBO)) { |
| 2078 |
if (match(X, m_And(m_Specific(Y), m_Value())) || |
2078 |
if (match(X, m_And(m_Specific(Y), m_Value())) || |
| 2079 |
match(X, m_And(m_Value(), m_Specific(Y)))) |
2079 |
match(X, m_And(m_Value(), m_Specific(Y)))) |
| 2080 |
if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q)) |
2080 |
if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q)) |
| 2081 |
return true; |
2081 |
return true; |
| 2082 |
if (match(Y, m_And(m_Specific(X), m_Value())) || |
2082 |
if (match(Y, m_And(m_Specific(X), m_Value())) || |
| 2083 |
match(Y, m_And(m_Value(), m_Specific(X)))) |
2083 |
match(Y, m_And(m_Value(), m_Specific(X)))) |
| 2084 |
if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q)) |
2084 |
if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q)) |
| 2085 |
return true; |
2085 |
return true; |
| 2086 |
|
2086 |
|
| 2087 |
unsigned BitWidth = V->getType()->getScalarSizeInBits(); |
2087 |
unsigned BitWidth = V->getType()->getScalarSizeInBits(); |
| 2088 |
KnownBits LHSBits(BitWidth); |
2088 |
KnownBits LHSBits(BitWidth); |
| 2089 |
computeKnownBits(X, LHSBits, Depth, Q); |
2089 |
computeKnownBits(X, LHSBits, Depth, Q); |
| 2090 |
|
2090 |
|
| 2091 |
KnownBits RHSBits(BitWidth); |
2091 |
KnownBits RHSBits(BitWidth); |
| 2092 |
computeKnownBits(Y, RHSBits, Depth, Q); |
2092 |
computeKnownBits(Y, RHSBits, Depth, Q); |
| 2093 |
// If i8 V is a power of two or zero: |
2093 |
// If i8 V is a power of two or zero: |
| 2094 |
// ZeroBits: 1 1 1 0 1 1 1 1 |
2094 |
// ZeroBits: 1 1 1 0 1 1 1 1 |
| 2095 |
// ~ZeroBits: 0 0 0 1 0 0 0 0 |
2095 |
// ~ZeroBits: 0 0 0 1 0 0 0 0 |
| 2096 |
if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2()) |
2096 |
if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2()) |
| 2097 |
// If OrZero isn't set, we cannot give back a zero result. |
2097 |
// If OrZero isn't set, we cannot give back a zero result. |
| 2098 |
// Make sure either the LHS or RHS has a bit set. |
2098 |
// Make sure either the LHS or RHS has a bit set. |
| 2099 |
if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue()) |
2099 |
if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue()) |
| 2100 |
return true; |
2100 |
return true; |
| 2101 |
} |
2101 |
} |
| 2102 |
} |
2102 |
} |
| 2103 |
|
2103 |
|
| 2104 |
// A PHI node is power of two if all incoming values are power of two, or if |
2104 |
// A PHI node is power of two if all incoming values are power of two, or if |
| 2105 |
// it is an induction variable where in each step its value is a power of two. |
2105 |
// it is an induction variable where in each step its value is a power of two. |
| 2106 |
if (const PHINode *PN = dyn_cast(V)) { |
2106 |
if (const PHINode *PN = dyn_cast(V)) { |
| 2107 |
SimplifyQuery RecQ = Q; |
2107 |
SimplifyQuery RecQ = Q; |
| 2108 |
|
2108 |
|
| 2109 |
// Check if it is an induction variable and always power of two. |
2109 |
// Check if it is an induction variable and always power of two. |
| 2110 |
if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ)) |
2110 |
if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ)) |
| 2111 |
return true; |
2111 |
return true; |
| 2112 |
|
2112 |
|
| 2113 |
// Recursively check all incoming values. Limit recursion to 2 levels, so |
2113 |
// Recursively check all incoming values. Limit recursion to 2 levels, so |
| 2114 |
// that search complexity is limited to number of operands^2. |
2114 |
// that search complexity is limited to number of operands^2. |
| 2115 |
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1); |
2115 |
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1); |
| 2116 |
return llvm::all_of(PN->operands(), [&](const Use &U) { |
2116 |
return llvm::all_of(PN->operands(), [&](const Use &U) { |
| 2117 |
// Value is power of 2 if it is coming from PHI node itself by induction. |
2117 |
// Value is power of 2 if it is coming from PHI node itself by induction. |
| 2118 |
if (U.get() == PN) |
2118 |
if (U.get() == PN) |
| 2119 |
return true; |
2119 |
return true; |
| 2120 |
|
2120 |
|
| 2121 |
// Change the context instruction to the incoming block where it is |
2121 |
// Change the context instruction to the incoming block where it is |
| 2122 |
// evaluated. |
2122 |
// evaluated. |
| 2123 |
RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator(); |
2123 |
RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator(); |
| 2124 |
return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ); |
2124 |
return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ); |
| 2125 |
}); |
2125 |
}); |
| 2126 |
} |
2126 |
} |
| 2127 |
|
2127 |
|
| 2128 |
// An exact divide or right shift can only shift off zero bits, so the result |
2128 |
// An exact divide or right shift can only shift off zero bits, so the result |
| 2129 |
// is a power of two only if the first operand is a power of two and not |
2129 |
// is a power of two only if the first operand is a power of two and not |
| 2130 |
// copying a sign bit (sdiv int_min, 2). |
2130 |
// copying a sign bit (sdiv int_min, 2). |
| 2131 |
if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) || |
2131 |
if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) || |
| 2132 |
match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) { |
2132 |
match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) { |
| 2133 |
return isKnownToBeAPowerOfTwo(cast(V)->getOperand(0), OrZero, |
2133 |
return isKnownToBeAPowerOfTwo(cast(V)->getOperand(0), OrZero, |
| 2134 |
Depth, Q); |
2134 |
Depth, Q); |
| 2135 |
} |
2135 |
} |
| 2136 |
|
2136 |
|
| 2137 |
return false; |
2137 |
return false; |
| 2138 |
} |
2138 |
} |
| 2139 |
|
2139 |
|
| 2140 |
/// Test whether a GEP's result is known to be non-null. |
2140 |
/// Test whether a GEP's result is known to be non-null. |
| 2141 |
/// |
2141 |
/// |
| 2142 |
/// Uses properties inherent in a GEP to try to determine whether it is known |
2142 |
/// Uses properties inherent in a GEP to try to determine whether it is known |
| 2143 |
/// to be non-null. |
2143 |
/// to be non-null. |
| 2144 |
/// |
2144 |
/// |
| 2145 |
/// Currently this routine does not support vector GEPs. |
2145 |
/// Currently this routine does not support vector GEPs. |
| 2146 |
static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, |
2146 |
static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, |
| 2147 |
const SimplifyQuery &Q) { |
2147 |
const SimplifyQuery &Q) { |
| 2148 |
const Function *F = nullptr; |
2148 |
const Function *F = nullptr; |
| 2149 |
if (const Instruction *I = dyn_cast(GEP)) |
2149 |
if (const Instruction *I = dyn_cast(GEP)) |
| 2150 |
F = I->getFunction(); |
2150 |
F = I->getFunction(); |
| 2151 |
|
2151 |
|
| 2152 |
if (!GEP->isInBounds() || |
2152 |
if (!GEP->isInBounds() || |
| 2153 |
NullPointerIsDefined(F, GEP->getPointerAddressSpace())) |
2153 |
NullPointerIsDefined(F, GEP->getPointerAddressSpace())) |
| 2154 |
return false; |
2154 |
return false; |
| 2155 |
|
2155 |
|
| 2156 |
// FIXME: Support vector-GEPs. |
2156 |
// FIXME: Support vector-GEPs. |
| 2157 |
assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP"); |
2157 |
assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP"); |
| 2158 |
|
2158 |
|
| 2159 |
// If the base pointer is non-null, we cannot walk to a null address with an |
2159 |
// If the base pointer is non-null, we cannot walk to a null address with an |
| 2160 |
// inbounds GEP in address space zero. |
2160 |
// inbounds GEP in address space zero. |
| 2161 |
if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q)) |
2161 |
if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q)) |
| 2162 |
return true; |
2162 |
return true; |
| 2163 |
|
2163 |
|
| 2164 |
// Walk the GEP operands and see if any operand introduces a non-zero offset. |
2164 |
// Walk the GEP operands and see if any operand introduces a non-zero offset. |
| 2165 |
// If so, then the GEP cannot produce a null pointer, as doing so would |
2165 |
// If so, then the GEP cannot produce a null pointer, as doing so would |
| 2166 |
// inherently violate the inbounds contract within address space zero. |
2166 |
// inherently violate the inbounds contract within address space zero. |
| 2167 |
for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); |
2167 |
for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); |
| 2168 |
GTI != GTE; ++GTI) { |
2168 |
GTI != GTE; ++GTI) { |
| 2169 |
// Struct types are easy -- they must always be indexed by a constant. |
2169 |
// Struct types are easy -- they must always be indexed by a constant. |
| 2170 |
if (StructType *STy = GTI.getStructTypeOrNull()) { |
2170 |
if (StructType *STy = GTI.getStructTypeOrNull()) { |
| 2171 |
ConstantInt *OpC = cast(GTI.getOperand()); |
2171 |
ConstantInt *OpC = cast(GTI.getOperand()); |
| 2172 |
unsigned ElementIdx = OpC->getZExtValue(); |
2172 |
unsigned ElementIdx = OpC->getZExtValue(); |
| 2173 |
const StructLayout *SL = Q.DL.getStructLayout(STy); |
2173 |
const StructLayout *SL = Q.DL.getStructLayout(STy); |
| 2174 |
uint64_t ElementOffset = SL->getElementOffset(ElementIdx); |
2174 |
uint64_t ElementOffset = SL->getElementOffset(ElementIdx); |
| 2175 |
if (ElementOffset > 0) |
2175 |
if (ElementOffset > 0) |
| 2176 |
return true; |
2176 |
return true; |
| 2177 |
continue; |
2177 |
continue; |
| 2178 |
} |
2178 |
} |
| 2179 |
|
2179 |
|
| 2180 |
// If we have a zero-sized type, the index doesn't matter. Keep looping. |
2180 |
// If we have a zero-sized type, the index doesn't matter. Keep looping. |
| 2181 |
if (Q.DL.getTypeAllocSize(GTI.getIndexedType()).isZero()) |
2181 |
if (Q.DL.getTypeAllocSize(GTI.getIndexedType()).isZero()) |
| 2182 |
continue; |
2182 |
continue; |
| 2183 |
|
2183 |
|
| 2184 |
// Fast path the constant operand case both for efficiency and so we don't |
2184 |
// Fast path the constant operand case both for efficiency and so we don't |
| 2185 |
// increment Depth when just zipping down an all-constant GEP. |
2185 |
// increment Depth when just zipping down an all-constant GEP. |
| 2186 |
if (ConstantInt *OpC = dyn_cast(GTI.getOperand())) { |
2186 |
if (ConstantInt *OpC = dyn_cast(GTI.getOperand())) { |
| 2187 |
if (!OpC->isZero()) |
2187 |
if (!OpC->isZero()) |
| 2188 |
return true; |
2188 |
return true; |
| 2189 |
continue; |
2189 |
continue; |
| 2190 |
} |
2190 |
} |
| 2191 |
|
2191 |
|
| 2192 |
// We post-increment Depth here because while isKnownNonZero increments it |
2192 |
// We post-increment Depth here because while isKnownNonZero increments it |
| 2193 |
// as well, when we pop back up that increment won't persist. We don't want |
2193 |
// as well, when we pop back up that increment won't persist. We don't want |
| 2194 |
// to recurse 10k times just because we have 10k GEP operands. We don't |
2194 |
// to recurse 10k times just because we have 10k GEP operands. We don't |
| 2195 |
// bail completely out because we want to handle constant GEPs regardless |
2195 |
// bail completely out because we want to handle constant GEPs regardless |
| 2196 |
// of depth. |
2196 |
// of depth. |
| 2197 |
if (Depth++ >= MaxAnalysisRecursionDepth) |
2197 |
if (Depth++ >= MaxAnalysisRecursionDepth) |
| 2198 |
continue; |
2198 |
continue; |
| 2199 |
|
2199 |
|
| 2200 |
if (isKnownNonZero(GTI.getOperand(), Depth, Q)) |
2200 |
if (isKnownNonZero(GTI.getOperand(), Depth, Q)) |
| 2201 |
return true; |
2201 |
return true; |
| 2202 |
} |
2202 |
} |
| 2203 |
|
2203 |
|
| 2204 |
return false; |
2204 |
return false; |
| 2205 |
} |
2205 |
} |
| 2206 |
|
2206 |
|
| 2207 |
static bool isKnownNonNullFromDominatingCondition(const Value *V, |
2207 |
static bool isKnownNonNullFromDominatingCondition(const Value *V, |
| 2208 |
const Instruction *CtxI, |
2208 |
const Instruction *CtxI, |
| 2209 |
const DominatorTree *DT) { |
2209 |
const DominatorTree *DT) { |
| 2210 |
assert(!isa(V) && "Called for constant?"); |
2210 |
assert(!isa(V) && "Called for constant?"); |
| 2211 |
|
2211 |
|
| 2212 |
if (!CtxI || !DT) |
2212 |
if (!CtxI || !DT) |
| 2213 |
return false; |
2213 |
return false; |
| 2214 |
|
2214 |
|
| 2215 |
unsigned NumUsesExplored = 0; |
2215 |
unsigned NumUsesExplored = 0; |
| 2216 |
for (const auto *U : V->users()) { |
2216 |
for (const auto *U : V->users()) { |
| 2217 |
// Avoid massive lists |
2217 |
// Avoid massive lists |
| 2218 |
if (NumUsesExplored >= DomConditionsMaxUses) |
2218 |
if (NumUsesExplored >= DomConditionsMaxUses) |
| 2219 |
break; |
2219 |
break; |
| 2220 |
NumUsesExplored++; |
2220 |
NumUsesExplored++; |
| 2221 |
|
2221 |
|
| 2222 |
// If the value is used as an argument to a call or invoke, then argument |
2222 |
// If the value is used as an argument to a call or invoke, then argument |
| 2223 |
// attributes may provide an answer about null-ness. |
2223 |
// attributes may provide an answer about null-ness. |
| 2224 |
if (const auto *CB = dyn_cast(U)) |
2224 |
if (const auto *CB = dyn_cast(U)) |
| 2225 |
if (auto *CalledFunc = CB->getCalledFunction()) |
2225 |
if (auto *CalledFunc = CB->getCalledFunction()) |
| 2226 |
for (const Argument &Arg : CalledFunc->args()) |
2226 |
for (const Argument &Arg : CalledFunc->args()) |
| 2227 |
if (CB->getArgOperand(Arg.getArgNo()) == V && |
2227 |
if (CB->getArgOperand(Arg.getArgNo()) == V && |
| 2228 |
Arg.hasNonNullAttr(/* AllowUndefOrPoison */ false) && |
2228 |
Arg.hasNonNullAttr(/* AllowUndefOrPoison */ false) && |
| 2229 |
DT->dominates(CB, CtxI)) |
2229 |
DT->dominates(CB, CtxI)) |
| 2230 |
return true; |
2230 |
return true; |
| 2231 |
|
2231 |
|
| 2232 |
// If the value is used as a load/store, then the pointer must be non null. |
2232 |
// If the value is used as a load/store, then the pointer must be non null. |
| 2233 |
if (V == getLoadStorePointerOperand(U)) { |
2233 |
if (V == getLoadStorePointerOperand(U)) { |
| 2234 |
const Instruction *I = cast(U); |
2234 |
const Instruction *I = cast(U); |
| 2235 |
if (!NullPointerIsDefined(I->getFunction(), |
2235 |
if (!NullPointerIsDefined(I->getFunction(), |
| 2236 |
V->getType()->getPointerAddressSpace()) && |
2236 |
V->getType()->getPointerAddressSpace()) && |
| 2237 |
DT->dominates(I, CtxI)) |
2237 |
DT->dominates(I, CtxI)) |
| 2238 |
return true; |
2238 |
return true; |
| 2239 |
} |
2239 |
} |
| 2240 |
|
2240 |
|
| 2241 |
// Consider only compare instructions uniquely controlling a branch |
2241 |
// Consider only compare instructions uniquely controlling a branch |
| 2242 |
Value *RHS; |
2242 |
Value *RHS; |
| 2243 |
CmpInst::Predicate Pred; |
2243 |
CmpInst::Predicate Pred; |
| 2244 |
if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS)))) |
2244 |
if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS)))) |
| 2245 |
continue; |
2245 |
continue; |
| 2246 |
|
2246 |
|
| 2247 |
bool NonNullIfTrue; |
2247 |
bool NonNullIfTrue; |
| 2248 |
if (cmpExcludesZero(Pred, RHS)) |
2248 |
if (cmpExcludesZero(Pred, RHS)) |
| 2249 |
NonNullIfTrue = true; |
2249 |
NonNullIfTrue = true; |
| 2250 |
else if (cmpExcludesZero(CmpInst::getInversePredicate(Pred), RHS)) |
2250 |
else if (cmpExcludesZero(CmpInst::getInversePredicate(Pred), RHS)) |
| 2251 |
NonNullIfTrue = false; |
2251 |
NonNullIfTrue = false; |
| 2252 |
else |
2252 |
else |
| 2253 |
continue; |
2253 |
continue; |
| 2254 |
|
2254 |
|
| 2255 |
SmallVector WorkList; |
2255 |
SmallVector WorkList; |
| 2256 |
SmallPtrSet Visited; |
2256 |
SmallPtrSet Visited; |
| 2257 |
for (const auto *CmpU : U->users()) { |
2257 |
for (const auto *CmpU : U->users()) { |
| 2258 |
assert(WorkList.empty() && "Should be!"); |
2258 |
assert(WorkList.empty() && "Should be!"); |
| 2259 |
if (Visited.insert(CmpU).second) |
2259 |
if (Visited.insert(CmpU).second) |
| 2260 |
WorkList.push_back(CmpU); |
2260 |
WorkList.push_back(CmpU); |
| 2261 |
|
2261 |
|
| 2262 |
while (!WorkList.empty()) { |
2262 |
while (!WorkList.empty()) { |
| 2263 |
auto *Curr = WorkList.pop_back_val(); |
2263 |
auto *Curr = WorkList.pop_back_val(); |
| 2264 |
|
2264 |
|
| 2265 |
// If a user is an AND, add all its users to the work list. We only |
2265 |
// If a user is an AND, add all its users to the work list. We only |
| 2266 |
// propagate "pred != null" condition through AND because it is only |
2266 |
// propagate "pred != null" condition through AND because it is only |
| 2267 |
// correct to assume that all conditions of AND are met in true branch. |
2267 |
// correct to assume that all conditions of AND are met in true branch. |
| 2268 |
// TODO: Support similar logic of OR and EQ predicate? |
2268 |
// TODO: Support similar logic of OR and EQ predicate? |
| 2269 |
if (NonNullIfTrue) |
2269 |
if (NonNullIfTrue) |
| 2270 |
if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) { |
2270 |
if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) { |
| 2271 |
for (const auto *CurrU : Curr->users()) |
2271 |
for (const auto *CurrU : Curr->users()) |
| 2272 |
if (Visited.insert(CurrU).second) |
2272 |
if (Visited.insert(CurrU).second) |
| 2273 |
WorkList.push_back(CurrU); |
2273 |
WorkList.push_back(CurrU); |
| 2274 |
continue; |
2274 |
continue; |
| 2275 |
} |
2275 |
} |
| 2276 |
|
2276 |
|
| 2277 |
if (const BranchInst *BI = dyn_cast(Curr)) { |
2277 |
if (const BranchInst *BI = dyn_cast(Curr)) { |
| 2278 |
assert(BI->isConditional() && "uses a comparison!"); |
2278 |
assert(BI->isConditional() && "uses a comparison!"); |
| 2279 |
|
2279 |
|
| 2280 |
BasicBlock *NonNullSuccessor = |
2280 |
BasicBlock *NonNullSuccessor = |
| 2281 |
BI->getSuccessor(NonNullIfTrue ? 0 : 1); |
2281 |
BI->getSuccessor(NonNullIfTrue ? 0 : 1); |
| 2282 |
BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor); |
2282 |
BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor); |
| 2283 |
if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent())) |
2283 |
if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent())) |
| 2284 |
return true; |
2284 |
return true; |
| 2285 |
} else if (NonNullIfTrue && isGuard(Curr) && |
2285 |
} else if (NonNullIfTrue && isGuard(Curr) && |
| 2286 |
DT->dominates(cast(Curr), CtxI)) { |
2286 |
DT->dominates(cast(Curr), CtxI)) { |
| 2287 |
return true; |
2287 |
return true; |
| 2288 |
} |
2288 |
} |
| 2289 |
} |
2289 |
} |
| 2290 |
} |
2290 |
} |
| 2291 |
} |
2291 |
} |
| 2292 |
|
2292 |
|
| 2293 |
return false; |
2293 |
return false; |
| 2294 |
} |
2294 |
} |
| 2295 |
|
2295 |
|
| 2296 |
/// Does the 'Range' metadata (which must be a valid MD_range operand list) |
2296 |
/// Does the 'Range' metadata (which must be a valid MD_range operand list) |
| 2297 |
/// ensure that the value it's attached to is never Value? 'RangeType' is |
2297 |
/// ensure that the value it's attached to is never Value? 'RangeType' is |
| 2298 |
/// is the type of the value described by the range. |
2298 |
/// is the type of the value described by the range. |
| 2299 |
static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) { |
2299 |
static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) { |
| 2300 |
const unsigned NumRanges = Ranges->getNumOperands() / 2; |
2300 |
const unsigned NumRanges = Ranges->getNumOperands() / 2; |
| 2301 |
assert(NumRanges >= 1); |
2301 |
assert(NumRanges >= 1); |
| 2302 |
for (unsigned i = 0; i < NumRanges; ++i) { |
2302 |
for (unsigned i = 0; i < NumRanges; ++i) { |
| 2303 |
ConstantInt *Lower = |
2303 |
ConstantInt *Lower = |
| 2304 |
mdconst::extract(Ranges->getOperand(2 * i + 0)); |
2304 |
mdconst::extract(Ranges->getOperand(2 * i + 0)); |
| 2305 |
ConstantInt *Upper = |
2305 |
ConstantInt *Upper = |
| 2306 |
mdconst::extract(Ranges->getOperand(2 * i + 1)); |
2306 |
mdconst::extract(Ranges->getOperand(2 * i + 1)); |
| 2307 |
ConstantRange Range(Lower->getValue(), Upper->getValue()); |
2307 |
ConstantRange Range(Lower->getValue(), Upper->getValue()); |
| 2308 |
if (Range.contains(Value)) |
2308 |
if (Range.contains(Value)) |
| 2309 |
return false; |
2309 |
return false; |
| 2310 |
} |
2310 |
} |
| 2311 |
return true; |
2311 |
return true; |
| 2312 |
} |
2312 |
} |
| 2313 |
|
2313 |
|
| 2314 |
/// Try to detect a recurrence that monotonically increases/decreases from a |
2314 |
/// Try to detect a recurrence that monotonically increases/decreases from a |
| 2315 |
/// non-zero starting value. These are common as induction variables. |
2315 |
/// non-zero starting value. These are common as induction variables. |
| 2316 |
static bool isNonZeroRecurrence(const PHINode *PN) { |
2316 |
static bool isNonZeroRecurrence(const PHINode *PN) { |
| 2317 |
BinaryOperator *BO = nullptr; |
2317 |
BinaryOperator *BO = nullptr; |
| 2318 |
Value *Start = nullptr, *Step = nullptr; |
2318 |
Value *Start = nullptr, *Step = nullptr; |
| 2319 |
const APInt *StartC, *StepC; |
2319 |
const APInt *StartC, *StepC; |
| 2320 |
if (!matchSimpleRecurrence(PN, BO, Start, Step) || |
2320 |
if (!matchSimpleRecurrence(PN, BO, Start, Step) || |
| 2321 |
!match(Start, m_APInt(StartC)) || StartC->isZero()) |
2321 |
!match(Start, m_APInt(StartC)) || StartC->isZero()) |
| 2322 |
return false; |
2322 |
return false; |
| 2323 |
|
2323 |
|
| 2324 |
switch (BO->getOpcode()) { |
2324 |
switch (BO->getOpcode()) { |
| 2325 |
case Instruction::Add: |
2325 |
case Instruction::Add: |
| 2326 |
// Starting from non-zero and stepping away from zero can never wrap back |
2326 |
// Starting from non-zero and stepping away from zero can never wrap back |
| 2327 |
// to zero. |
2327 |
// to zero. |
| 2328 |
return BO->hasNoUnsignedWrap() || |
2328 |
return BO->hasNoUnsignedWrap() || |
| 2329 |
(BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) && |
2329 |
(BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) && |
| 2330 |
StartC->isNegative() == StepC->isNegative()); |
2330 |
StartC->isNegative() == StepC->isNegative()); |
| 2331 |
case Instruction::Mul: |
2331 |
case Instruction::Mul: |
| 2332 |
return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) && |
2332 |
return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) && |
| 2333 |
match(Step, m_APInt(StepC)) && !StepC->isZero(); |
2333 |
match(Step, m_APInt(StepC)) && !StepC->isZero(); |
| 2334 |
case Instruction::Shl: |
2334 |
case Instruction::Shl: |
| 2335 |
return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap(); |
2335 |
return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap(); |
| 2336 |
case Instruction::AShr: |
2336 |
case Instruction::AShr: |
| 2337 |
case Instruction::LShr: |
2337 |
case Instruction::LShr: |
| 2338 |
return BO->isExact(); |
2338 |
return BO->isExact(); |
| 2339 |
default: |
2339 |
default: |
| 2340 |
return false; |
2340 |
return false; |
| 2341 |
} |
2341 |
} |
| 2342 |
} |
2342 |
} |
| 2343 |
|
2343 |
|
| 2344 |
static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth, |
2344 |
static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth, |
| 2345 |
const SimplifyQuery &Q, unsigned BitWidth, Value *X, |
2345 |
const SimplifyQuery &Q, unsigned BitWidth, Value *X, |
| 2346 |
Value *Y, bool NSW) { |
2346 |
Value *Y, bool NSW) { |
| 2347 |
KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q); |
2347 |
KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q); |
| 2348 |
KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q); |
2348 |
KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q); |
| 2349 |
|
2349 |
|
| 2350 |
// If X and Y are both non-negative (as signed values) then their sum is not |
2350 |
// If X and Y are both non-negative (as signed values) then their sum is not |
| 2351 |
// zero unless both X and Y are zero. |
2351 |
// zero unless both X and Y are zero. |
| 2352 |
if (XKnown.isNonNegative() && YKnown.isNonNegative()) |
2352 |
if (XKnown.isNonNegative() && YKnown.isNonNegative()) |
| 2353 |
if (isKnownNonZero(Y, DemandedElts, Depth, Q) || |
2353 |
if (isKnownNonZero(Y, DemandedElts, Depth, Q) || |
| 2354 |
isKnownNonZero(X, DemandedElts, Depth, Q)) |
2354 |
isKnownNonZero(X, DemandedElts, Depth, Q)) |
| 2355 |
return true; |
2355 |
return true; |
| 2356 |
|
2356 |
|
| 2357 |
// If X and Y are both negative (as signed values) then their sum is not |
2357 |
// If X and Y are both negative (as signed values) then their sum is not |
| 2358 |
// zero unless both X and Y equal INT_MIN. |
2358 |
// zero unless both X and Y equal INT_MIN. |
| 2359 |
if (XKnown.isNegative() && YKnown.isNegative()) { |
2359 |
if (XKnown.isNegative() && YKnown.isNegative()) { |
| 2360 |
APInt Mask = APInt::getSignedMaxValue(BitWidth); |
2360 |
APInt Mask = APInt::getSignedMaxValue(BitWidth); |
| 2361 |
// The sign bit of X is set. If some other bit is set then X is not equal |
2361 |
// The sign bit of X is set. If some other bit is set then X is not equal |
| 2362 |
// to INT_MIN. |
2362 |
// to INT_MIN. |
| 2363 |
if (XKnown.One.intersects(Mask)) |
2363 |
if (XKnown.One.intersects(Mask)) |
| 2364 |
return true; |
2364 |
return true; |
| 2365 |
// The sign bit of Y is set. If some other bit is set then Y is not equal |
2365 |
// The sign bit of Y is set. If some other bit is set then Y is not equal |
| 2366 |
// to INT_MIN. |
2366 |
// to INT_MIN. |
| 2367 |
if (YKnown.One.intersects(Mask)) |
2367 |
if (YKnown.One.intersects(Mask)) |
| 2368 |
return true; |
2368 |
return true; |
| 2369 |
} |
2369 |
} |
| 2370 |
|
2370 |
|
| 2371 |
// The sum of a non-negative number and a power of two is not zero. |
2371 |
// The sum of a non-negative number and a power of two is not zero. |
| 2372 |
if (XKnown.isNonNegative() && |
2372 |
if (XKnown.isNonNegative() && |
| 2373 |
isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q)) |
2373 |
isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q)) |
| 2374 |
return true; |
2374 |
return true; |
| 2375 |
if (YKnown.isNonNegative() && |
2375 |
if (YKnown.isNonNegative() && |
| 2376 |
isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q)) |
2376 |
isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q)) |
| 2377 |
return true; |
2377 |
return true; |
| 2378 |
|
2378 |
|
| 2379 |
return KnownBits::computeForAddSub(/*Add*/ true, NSW, XKnown, YKnown) |
2379 |
return KnownBits::computeForAddSub(/*Add*/ true, NSW, XKnown, YKnown) |
| 2380 |
.isNonZero(); |
2380 |
.isNonZero(); |
| 2381 |
} |
2381 |
} |
| 2382 |
|
2382 |
|
| 2383 |
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth, |
2383 |
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth, |
| 2384 |
const SimplifyQuery &Q, unsigned BitWidth, Value *X, |
2384 |
const SimplifyQuery &Q, unsigned BitWidth, Value *X, |
| 2385 |
Value *Y) { |
2385 |
Value *Y) { |
| 2386 |
if (auto *C = dyn_cast(X)) |
2386 |
if (auto *C = dyn_cast(X)) |
| 2387 |
if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Depth, Q)) |
2387 |
if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Depth, Q)) |
| 2388 |
return true; |
2388 |
return true; |
| 2389 |
|
2389 |
|
| 2390 |
KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q); |
2390 |
KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q); |
| 2391 |
if (XKnown.isUnknown()) |
2391 |
if (XKnown.isUnknown()) |
| 2392 |
return false; |
2392 |
return false; |
| 2393 |
KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q); |
2393 |
KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q); |
| 2394 |
// If X != Y then X - Y is non zero. |
2394 |
// If X != Y then X - Y is non zero. |
| 2395 |
std::optional ne = KnownBits::ne(XKnown, YKnown); |
2395 |
std::optional ne = KnownBits::ne(XKnown, YKnown); |
| 2396 |
// If we are unable to compute if X != Y, we won't be able to do anything |
2396 |
// If we are unable to compute if X != Y, we won't be able to do anything |
| 2397 |
// computing the knownbits of the sub expression so just return here. |
2397 |
// computing the knownbits of the sub expression so just return here. |
| 2398 |
return ne && *ne; |
2398 |
return ne && *ne; |
| 2399 |
} |
2399 |
} |
| 2400 |
|
2400 |
|
| 2401 |
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, |
2401 |
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, |
| 2402 |
unsigned Depth, const SimplifyQuery &Q, |
2402 |
unsigned Depth, const SimplifyQuery &Q, |
| 2403 |
const KnownBits &KnownVal) { |
2403 |
const KnownBits &KnownVal) { |
| 2404 |
auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) { |
2404 |
auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) { |
| 2405 |
switch (I->getOpcode()) { |
2405 |
switch (I->getOpcode()) { |
| 2406 |
case Instruction::Shl: |
2406 |
case Instruction::Shl: |
| 2407 |
return Lhs.shl(Rhs); |
2407 |
return Lhs.shl(Rhs); |
| 2408 |
case Instruction::LShr: |
2408 |
case Instruction::LShr: |
| 2409 |
return Lhs.lshr(Rhs); |
2409 |
return Lhs.lshr(Rhs); |
| 2410 |
case Instruction::AShr: |
2410 |
case Instruction::AShr: |
| 2411 |
return Lhs.ashr(Rhs); |
2411 |
return Lhs.ashr(Rhs); |
| 2412 |
default: |
2412 |
default: |
| 2413 |
llvm_unreachable("Unknown Shift Opcode"); |
2413 |
llvm_unreachable("Unknown Shift Opcode"); |
| 2414 |
} |
2414 |
} |
| 2415 |
}; |
2415 |
}; |
| 2416 |
|
2416 |
|
| 2417 |
auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) { |
2417 |
auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) { |
| 2418 |
switch (I->getOpcode()) { |
2418 |
switch (I->getOpcode()) { |
| 2419 |
case Instruction::Shl: |
2419 |
case Instruction::Shl: |
| 2420 |
return Lhs.lshr(Rhs); |
2420 |
return Lhs.lshr(Rhs); |
| 2421 |
case Instruction::LShr: |
2421 |
case Instruction::LShr: |
| 2422 |
case Instruction::AShr: |
2422 |
case Instruction::AShr: |
| 2423 |
return Lhs.shl(Rhs); |
2423 |
return Lhs.shl(Rhs); |
| 2424 |
default: |
2424 |
default: |
| 2425 |
llvm_unreachable("Unknown Shift Opcode"); |
2425 |
llvm_unreachable("Unknown Shift Opcode"); |
| 2426 |
} |
2426 |
} |
| 2427 |
}; |
2427 |
}; |
| 2428 |
|
2428 |
|
| 2429 |
if (KnownVal.isUnknown()) |
2429 |
if (KnownVal.isUnknown()) |
| 2430 |
return false; |
2430 |
return false; |
| 2431 |
|
2431 |
|
| 2432 |
KnownBits KnownCnt = |
2432 |
KnownBits KnownCnt = |
| 2433 |
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q); |
2433 |
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q); |
| 2434 |
APInt MaxShift = KnownCnt.getMaxValue(); |
2434 |
APInt MaxShift = KnownCnt.getMaxValue(); |
| 2435 |
unsigned NumBits = KnownVal.getBitWidth(); |
2435 |
unsigned NumBits = KnownVal.getBitWidth(); |
| 2436 |
if (MaxShift.uge(NumBits)) |
2436 |
if (MaxShift.uge(NumBits)) |
| 2437 |
return false; |
2437 |
return false; |
| 2438 |
|
2438 |
|
| 2439 |
if (!ShiftOp(KnownVal.One, MaxShift).isZero()) |
2439 |
if (!ShiftOp(KnownVal.One, MaxShift).isZero()) |
| 2440 |
return true; |
2440 |
return true; |
| 2441 |
|
2441 |
|
| 2442 |
// If all of the bits shifted out are known to be zero, and Val is known |
2442 |
// If all of the bits shifted out are known to be zero, and Val is known |
| 2443 |
// non-zero then at least one non-zero bit must remain. |
2443 |
// non-zero then at least one non-zero bit must remain. |
| 2444 |
if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift) |
2444 |
if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift) |
| 2445 |
.eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) && |
2445 |
.eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) && |
| 2446 |
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q)) |
2446 |
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q)) |
| 2447 |
return true; |
2447 |
return true; |
| 2448 |
|
2448 |
|
| 2449 |
return false; |
2449 |
return false; |
| 2450 |
} |
2450 |
} |
| 2451 |
|
2451 |
|
| 2452 |
static bool isKnownNonZeroFromOperator(const Operator *I, |
2452 |
static bool isKnownNonZeroFromOperator(const Operator *I, |
| 2453 |
const APInt &DemandedElts, |
2453 |
const APInt &DemandedElts, |
| 2454 |
unsigned Depth, const SimplifyQuery &Q) { |
2454 |
unsigned Depth, const SimplifyQuery &Q) { |
| 2455 |
unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL); |
2455 |
unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL); |
| 2456 |
switch (I->getOpcode()) { |
2456 |
switch (I->getOpcode()) { |
| 2457 |
case Instruction::Alloca: |
2457 |
case Instruction::Alloca: |
| 2458 |
// Alloca never returns null, malloc might. |
2458 |
// Alloca never returns null, malloc might. |
| 2459 |
return I->getType()->getPointerAddressSpace() == 0; |
2459 |
return I->getType()->getPointerAddressSpace() == 0; |
| 2460 |
case Instruction::GetElementPtr: |
2460 |
case Instruction::GetElementPtr: |
| 2461 |
if (I->getType()->isPointerTy()) |
2461 |
if (I->getType()->isPointerTy()) |
| 2462 |
return isGEPKnownNonNull(cast(I), Depth, Q); |
2462 |
return isGEPKnownNonNull(cast(I), Depth, Q); |
| 2463 |
break; |
2463 |
break; |
| 2464 |
case Instruction::BitCast: { |
2464 |
case Instruction::BitCast: { |
| 2465 |
// We need to be a bit careful here. We can only peek through the bitcast |
2465 |
// We need to be a bit careful here. We can only peek through the bitcast |
| 2466 |
// if the scalar size of elements in the operand are smaller than and a |
2466 |
// if the scalar size of elements in the operand are smaller than and a |
| 2467 |
// multiple of the size they are casting too. Take three cases: |
2467 |
// multiple of the size they are casting too. Take three cases: |
| 2468 |
// |
2468 |
// |
| 2469 |
// 1) Unsafe: |
2469 |
// 1) Unsafe: |
| 2470 |
// bitcast <2 x i16> %NonZero to <4 x i8> |
2470 |
// bitcast <2 x i16> %NonZero to <4 x i8> |
| 2471 |
// |
2471 |
// |
| 2472 |
// %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a |
2472 |
// %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a |
| 2473 |
// <4 x i8> requires that all 4 i8 elements be non-zero which isn't |
2473 |
// <4 x i8> requires that all 4 i8 elements be non-zero which isn't |
| 2474 |
// guranteed (imagine just sign bit set in the 2 i16 elements). |
2474 |
// guranteed (imagine just sign bit set in the 2 i16 elements). |
| 2475 |
// |
2475 |
// |
| 2476 |
// 2) Unsafe: |
2476 |
// 2) Unsafe: |
| 2477 |
// bitcast <4 x i3> %NonZero to <3 x i4> |
2477 |
// bitcast <4 x i3> %NonZero to <3 x i4> |
| 2478 |
// |
2478 |
// |
| 2479 |
// Even though the scalar size of the src (`i3`) is smaller than the |
2479 |
// Even though the scalar size of the src (`i3`) is smaller than the |
| 2480 |
// scalar size of the dst `i4`, because `i3` is not a multiple of `i4` |
2480 |
// scalar size of the dst `i4`, because `i3` is not a multiple of `i4` |
| 2481 |
// its possible for the `3 x i4` elements to be zero because there are |
2481 |
// its possible for the `3 x i4` elements to be zero because there are |
| 2482 |
// some elements in the destination that don't contain any full src |
2482 |
// some elements in the destination that don't contain any full src |
| 2483 |
// element. |
2483 |
// element. |
| 2484 |
// |
2484 |
// |
| 2485 |
// 3) Safe: |
2485 |
// 3) Safe: |
| 2486 |
// bitcast <4 x i8> %NonZero to <2 x i16> |
2486 |
// bitcast <4 x i8> %NonZero to <2 x i16> |
| 2487 |
// |
2487 |
// |
| 2488 |
// This is always safe as non-zero in the 4 i8 elements implies |
2488 |
// This is always safe as non-zero in the 4 i8 elements implies |
| 2489 |
// non-zero in the combination of any two adjacent ones. Since i8 is a |
2489 |
// non-zero in the combination of any two adjacent ones. Since i8 is a |
| 2490 |
// multiple of i16, each i16 is guranteed to have 2 full i8 elements. |
2490 |
// multiple of i16, each i16 is guranteed to have 2 full i8 elements. |
| 2491 |
// This all implies the 2 i16 elements are non-zero. |
2491 |
// This all implies the 2 i16 elements are non-zero. |
| 2492 |
Type *FromTy = I->getOperand(0)->getType(); |
2492 |
Type *FromTy = I->getOperand(0)->getType(); |
| 2493 |
if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) && |
2493 |
if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) && |
| 2494 |
(BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0) |
2494 |
(BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0) |
| 2495 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
2495 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
| 2496 |
} break; |
2496 |
} break; |
| 2497 |
case Instruction::IntToPtr: |
2497 |
case Instruction::IntToPtr: |
| 2498 |
// Note that we have to take special care to avoid looking through |
2498 |
// Note that we have to take special care to avoid looking through |
| 2499 |
// truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well |
2499 |
// truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well |
| 2500 |
// as casts that can alter the value, e.g., AddrSpaceCasts. |
2500 |
// as casts that can alter the value, e.g., AddrSpaceCasts. |
| 2501 |
if (!isa(I->getType()) && |
2501 |
if (!isa(I->getType()) && |
| 2502 |
Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <= |
2502 |
Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <= |
| 2503 |
Q.DL.getTypeSizeInBits(I->getType()).getFixedValue()) |
2503 |
Q.DL.getTypeSizeInBits(I->getType()).getFixedValue()) |
| 2504 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
2504 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
| 2505 |
break; |
2505 |
break; |
| 2506 |
case Instruction::PtrToInt: |
2506 |
case Instruction::PtrToInt: |
| 2507 |
// Similar to int2ptr above, we can look through ptr2int here if the cast |
2507 |
// Similar to int2ptr above, we can look through ptr2int here if the cast |
| 2508 |
// is a no-op or an extend and not a truncate. |
2508 |
// is a no-op or an extend and not a truncate. |
| 2509 |
if (!isa(I->getType()) && |
2509 |
if (!isa(I->getType()) && |
| 2510 |
Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <= |
2510 |
Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <= |
| 2511 |
Q.DL.getTypeSizeInBits(I->getType()).getFixedValue()) |
2511 |
Q.DL.getTypeSizeInBits(I->getType()).getFixedValue()) |
| 2512 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
2512 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
| 2513 |
break; |
2513 |
break; |
| 2514 |
case Instruction::Sub: |
2514 |
case Instruction::Sub: |
| 2515 |
return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0), |
2515 |
return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0), |
| 2516 |
I->getOperand(1)); |
2516 |
I->getOperand(1)); |
| 2517 |
case Instruction::Or: |
2517 |
case Instruction::Or: |
| 2518 |
// X | Y != 0 if X != 0 or Y != 0. |
2518 |
// X | Y != 0 if X != 0 or Y != 0. |
| 2519 |
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q) || |
2519 |
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q) || |
| 2520 |
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); |
2520 |
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); |
| 2521 |
case Instruction::SExt: |
2521 |
case Instruction::SExt: |
| 2522 |
case Instruction::ZExt: |
2522 |
case Instruction::ZExt: |
| 2523 |
// ext X != 0 if X != 0. |
2523 |
// ext X != 0 if X != 0. |
| 2524 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
2524 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
| 2525 |
|
2525 |
|
| 2526 |
case Instruction::Shl: { |
2526 |
case Instruction::Shl: { |
| 2527 |
// shl nsw/nuw can't remove any non-zero bits. |
2527 |
// shl nsw/nuw can't remove any non-zero bits. |
| 2528 |
const OverflowingBinaryOperator *BO = cast(I); |
2528 |
const OverflowingBinaryOperator *BO = cast(I); |
| 2529 |
if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO)) |
2529 |
if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO)) |
| 2530 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
2530 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
| 2531 |
|
2531 |
|
| 2532 |
// shl X, Y != 0 if X is odd. Note that the value of the shift is undefined |
2532 |
// shl X, Y != 0 if X is odd. Note that the value of the shift is undefined |
| 2533 |
// if the lowest bit is shifted off the end. |
2533 |
// if the lowest bit is shifted off the end. |
| 2534 |
KnownBits Known(BitWidth); |
2534 |
KnownBits Known(BitWidth); |
| 2535 |
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q); |
2535 |
computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q); |
| 2536 |
if (Known.One[0]) |
2536 |
if (Known.One[0]) |
| 2537 |
return true; |
2537 |
return true; |
| 2538 |
|
2538 |
|
| 2539 |
return isNonZeroShift(I, DemandedElts, Depth, Q, Known); |
2539 |
return isNonZeroShift(I, DemandedElts, Depth, Q, Known); |
| 2540 |
} |
2540 |
} |
| 2541 |
case Instruction::LShr: |
2541 |
case Instruction::LShr: |
| 2542 |
case Instruction::AShr: { |
2542 |
case Instruction::AShr: { |
| 2543 |
// shr exact can only shift out zero bits. |
2543 |
// shr exact can only shift out zero bits. |
| 2544 |
const PossiblyExactOperator *BO = cast(I); |
2544 |
const PossiblyExactOperator *BO = cast(I); |
| 2545 |
if (BO->isExact()) |
2545 |
if (BO->isExact()) |
| 2546 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
2546 |
return isKnownNonZero(I->getOperand(0), Depth, Q); |
| 2547 |
|
2547 |
|
| 2548 |
// shr X, Y != 0 if X is negative. Note that the value of the shift is not |
2548 |
// shr X, Y != 0 if X is negative. Note that the value of the shift is not |
| 2549 |
// defined if the sign bit is shifted off the end. |
2549 |
// defined if the sign bit is shifted off the end. |
| 2550 |
KnownBits Known = |
2550 |
KnownBits Known = |
| 2551 |
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q); |
2551 |
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q); |
| 2552 |
if (Known.isNegative()) |
2552 |
if (Known.isNegative()) |
| 2553 |
return true; |
2553 |
return true; |
| 2554 |
|
2554 |
|
| 2555 |
return isNonZeroShift(I, DemandedElts, Depth, Q, Known); |
2555 |
return isNonZeroShift(I, DemandedElts, Depth, Q, Known); |
| 2556 |
} |
2556 |
} |
| 2557 |
case Instruction::UDiv: |
2557 |
case Instruction::UDiv: |
| 2558 |
case Instruction::SDiv: |
2558 |
case Instruction::SDiv: |
| 2559 |
// X / Y |
2559 |
// X / Y |
| 2560 |
// div exact can only produce a zero if the dividend is zero. |
2560 |
// div exact can only produce a zero if the dividend is zero. |
| 2561 |
if (cast(I)->isExact()) |
2561 |
if (cast(I)->isExact()) |
| 2562 |
return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); |
2562 |
return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); |
| 2563 |
if (I->getOpcode() == Instruction::UDiv) { |
2563 |
if (I->getOpcode() == Instruction::UDiv) { |
| 2564 |
std::optional XUgeY; |
2564 |
std::optional XUgeY; |
| 2565 |
KnownBits XKnown = |
2565 |
KnownBits XKnown = |
| 2566 |
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q); |
2566 |
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q); |
| 2567 |
if (!XKnown.isUnknown()) { |
2567 |
if (!XKnown.isUnknown()) { |
| 2568 |
KnownBits YKnown = |
2568 |
KnownBits YKnown = |
| 2569 |
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q); |
2569 |
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q); |
| 2570 |
// If X u>= Y then div is non zero (0/0 is UB). |
2570 |
// If X u>= Y then div is non zero (0/0 is UB). |
| 2571 |
XUgeY = KnownBits::uge(XKnown, YKnown); |
2571 |
XUgeY = KnownBits::uge(XKnown, YKnown); |
| 2572 |
} |
2572 |
} |
| 2573 |
// If X is total unknown or X u< Y we won't be able to prove non-zero |
2573 |
// If X is total unknown or X u< Y we won't be able to prove non-zero |
| 2574 |
// with compute known bits so just return early. |
2574 |
// with compute known bits so just return early. |
| 2575 |
return XUgeY && *XUgeY; |
2575 |
return XUgeY && *XUgeY; |
| 2576 |
} |
2576 |
} |
| 2577 |
break; |
2577 |
break; |
| 2578 |
case Instruction::Add: { |
2578 |
case Instruction::Add: { |
| 2579 |
// X + Y. |
2579 |
// X + Y. |
| 2580 |
|
2580 |
|
| 2581 |
// If Add has nuw wrap flag, then if either X or Y is non-zero the result is |
2581 |
// If Add has nuw wrap flag, then if either X or Y is non-zero the result is |
| 2582 |
// non-zero. |
2582 |
// non-zero. |
| 2583 |
auto *BO = cast(I); |
2583 |
auto *BO = cast(I); |
| 2584 |
if (Q.IIQ.hasNoUnsignedWrap(BO)) |
2584 |
if (Q.IIQ.hasNoUnsignedWrap(BO)) |
| 2585 |
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q) || |
2585 |
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q) || |
| 2586 |
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); |
2586 |
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); |
| 2587 |
|
2587 |
|
| 2588 |
return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0), |
2588 |
return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0), |
| 2589 |
I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO)); |
2589 |
I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO)); |
| 2590 |
} |
2590 |
} |
| 2591 |
case Instruction::Mul: { |
2591 |
case Instruction::Mul: { |
| 2592 |
// If X and Y are non-zero then so is X * Y as long as the multiplication |
2592 |
// If X and Y are non-zero then so is X * Y as long as the multiplication |
| 2593 |
// does not overflow. |
2593 |
// does not overflow. |
| 2594 |
const OverflowingBinaryOperator *BO = cast(I); |
2594 |
const OverflowingBinaryOperator *BO = cast(I); |
| 2595 |
if (Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) |
2595 |
if (Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) |
| 2596 |
return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) && |
2596 |
return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) && |
| 2597 |
isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q); |
2597 |
isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q); |
| 2598 |
|
2598 |
|
| 2599 |
// If either X or Y is odd, then if the other is non-zero the result can't |
2599 |
// If either X or Y is odd, then if the other is non-zero the result can't |
| 2600 |
// be zero. |
2600 |
// be zero. |
| 2601 |
KnownBits XKnown = |
2601 |
KnownBits XKnown = |
| 2602 |
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q); |
2602 |
computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q); |
| 2603 |
if (XKnown.One[0]) |
2603 |
if (XKnown.One[0]) |
| 2604 |
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q); |
2604 |
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q); |
| 2605 |
|
2605 |
|
| 2606 |
KnownBits YKnown = |
2606 |
KnownBits YKnown = |
| 2607 |
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q); |
2607 |
computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q); |
| 2608 |
if (YKnown.One[0]) |
2608 |
if (YKnown.One[0]) |
| 2609 |
return XKnown.isNonZero() || |
2609 |
return XKnown.isNonZero() || |
| 2610 |
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); |
2610 |
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q); |
| 2611 |
|
2611 |
|
| 2612 |
// If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is |
2612 |
// If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is |
| 2613 |
// non-zero, then X * Y is non-zero. We can find sX and sY by just taking |
2613 |
// non-zero, then X * Y is non-zero. We can find sX and sY by just taking |
| 2614 |
// the lowest known One of X and Y. If they are non-zero, the result |
2614 |
// the lowest known One of X and Y. If they are non-zero, the result |
| 2615 |
// must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing |
2615 |
// must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing |
| 2616 |
// X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth. |
2616 |
// X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth. |
| 2617 |
return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) < |
2617 |
return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) < |
| 2618 |
BitWidth; |
2618 |
BitWidth; |
| 2619 |
} |
2619 |
} |
| 2620 |
case Instruction::Select: { |
2620 |
case Instruction::Select: { |
| 2621 |
// (C ? X : Y) != 0 if X != 0 and Y != 0. |
2621 |
// (C ? X : Y) != 0 if X != 0 and Y != 0. |
| 2622 |
|
2622 |
|
| 2623 |
// First check if the arm is non-zero using `isKnownNonZero`. If that fails, |
2623 |
// First check if the arm is non-zero using `isKnownNonZero`. If that fails, |
| 2624 |
// then see if the select condition implies the arm is non-zero. For example |
2624 |
// then see if the select condition implies the arm is non-zero. For example |
| 2625 |
// (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is |
2625 |
// (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is |
| 2626 |
// dominated by `X != 0`. |
2626 |
// dominated by `X != 0`. |
| 2627 |
auto SelectArmIsNonZero = [&](bool IsTrueArm) { |
2627 |
auto SelectArmIsNonZero = [&](bool IsTrueArm) { |
| 2628 |
Value *Op; |
2628 |
Value *Op; |
| 2629 |
Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2); |
2629 |
Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2); |
| 2630 |
// Op is trivially non-zero. |
2630 |
// Op is trivially non-zero. |
| 2631 |
if (isKnownNonZero(Op, DemandedElts, Depth, Q)) |
2631 |
if (isKnownNonZero(Op, DemandedElts, Depth, Q)) |
| 2632 |
return true; |
2632 |
return true; |
| 2633 |
|
2633 |
|
| 2634 |
// The condition of the select dominates the true/false arm. Check if the |
2634 |
// The condition of the select dominates the true/false arm. Check if the |
| 2635 |
// condition implies that a given arm is non-zero. |
2635 |
// condition implies that a given arm is non-zero. |
| 2636 |
Value *X; |
2636 |
Value *X; |
| 2637 |
CmpInst::Predicate Pred; |
2637 |
CmpInst::Predicate Pred; |
| 2638 |
if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X)))) |
2638 |
if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X)))) |
| 2639 |
return false; |
2639 |
return false; |
| 2640 |
|
2640 |
|
| 2641 |
if (!IsTrueArm) |
2641 |
if (!IsTrueArm) |
| 2642 |
Pred = ICmpInst::getInversePredicate(Pred); |
2642 |
Pred = ICmpInst::getInversePredicate(Pred); |
| 2643 |
|
2643 |
|
| 2644 |
return cmpExcludesZero(Pred, X); |
2644 |
return cmpExcludesZero(Pred, X); |
| 2645 |
}; |
2645 |
}; |
| 2646 |
|
2646 |
|
| 2647 |
if (SelectArmIsNonZero(/* IsTrueArm */ true) && |
2647 |
if (SelectArmIsNonZero(/* IsTrueArm */ true) && |
| 2648 |
SelectArmIsNonZero(/* IsTrueArm */ false)) |
2648 |
SelectArmIsNonZero(/* IsTrueArm */ false)) |
| 2649 |
return true; |
2649 |
return true; |
| 2650 |
break; |
2650 |
break; |
| 2651 |
} |
2651 |
} |
| 2652 |
case Instruction::PHI: { |
2652 |
case Instruction::PHI: { |
| 2653 |
auto *PN = cast(I); |
2653 |
auto *PN = cast(I); |
| 2654 |
if (Q.IIQ.UseInstrInfo && isNonZeroRecurrence(PN)) |
2654 |
if (Q.IIQ.UseInstrInfo && isNonZeroRecurrence(PN)) |
| 2655 |
return true; |
2655 |
return true; |
| 2656 |
|
2656 |
|
| 2657 |
// Check if all incoming values are non-zero using recursion. |
2657 |
// Check if all incoming values are non-zero using recursion. |
| 2658 |
SimplifyQuery RecQ = Q; |
2658 |
SimplifyQuery RecQ = Q; |
| 2659 |
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1); |
2659 |
unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1); |
| 2660 |
return llvm::all_of(PN->operands(), [&](const Use &U) { |
2660 |
return llvm::all_of(PN->operands(), [&](const Use &U) { |
| 2661 |
if (U.get() == PN) |
2661 |
if (U.get() == PN) |
| 2662 |
return true; |
2662 |
return true; |
| 2663 |
RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator(); |
2663 |
RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator(); |
| 2664 |
return isKnownNonZero(U.get(), DemandedElts, NewDepth, RecQ); |
2664 |
return isKnownNonZero(U.get(), DemandedElts, NewDepth, RecQ); |
| 2665 |
}); |
2665 |
}); |
| 2666 |
} |
2666 |
} |
| 2667 |
case Instruction::ExtractElement: |
2667 |
case Instruction::ExtractElement: |
| 2668 |
if (const auto *EEI = dyn_cast(I)) { |
2668 |
if (const auto *EEI = dyn_cast(I)) { |
| 2669 |
const Value *Vec = EEI->getVectorOperand(); |
2669 |
const Value *Vec = EEI->getVectorOperand(); |
| 2670 |
const Value *Idx = EEI->getIndexOperand(); |
2670 |
const Value *Idx = EEI->getIndexOperand(); |
| 2671 |
auto *CIdx = dyn_cast(Idx); |
2671 |
auto *CIdx = dyn_cast(Idx); |
| 2672 |
if (auto *VecTy = dyn_cast(Vec->getType())) { |
2672 |
if (auto *VecTy = dyn_cast(Vec->getType())) { |
| 2673 |
unsigned NumElts = VecTy->getNumElements(); |
2673 |
unsigned NumElts = VecTy->getNumElements(); |
| 2674 |
APInt DemandedVecElts = APInt::getAllOnes(NumElts); |
2674 |
APInt DemandedVecElts = APInt::getAllOnes(NumElts); |
| 2675 |
if (CIdx && CIdx->getValue().ult(NumElts)) |
2675 |
if (CIdx && CIdx->getValue().ult(NumElts)) |
| 2676 |
DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue()); |
2676 |
DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue()); |
| 2677 |
return isKnownNonZero(Vec, DemandedVecElts, Depth, Q); |
2677 |
return isKnownNonZero(Vec, DemandedVecElts, Depth, Q); |
| 2678 |
} |
2678 |
} |
| 2679 |
} |
2679 |
} |
| 2680 |
break; |
2680 |
break; |
| 2681 |
case Instruction::Freeze: |
2681 |
case Instruction::Freeze: |
| 2682 |
return isKnownNonZero(I->getOperand(0), Depth, Q) && |
2682 |
return isKnownNonZero(I->getOperand(0), Depth, Q) && |
| 2683 |
isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT, |
2683 |
isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT, |
| 2684 |
Depth); |
2684 |
Depth); |
| 2685 |
case Instruction::Load: |
2685 |
case Instruction::Load: |
| 2686 |
// A Load tagged with nonnull metadata is never null. |
2686 |
// A Load tagged with nonnull metadata is never null. |
| 2687 |
if (Q.IIQ.getMetadata(cast(I), LLVMContext::MD_nonnull)) |
2687 |
if (Q.IIQ.getMetadata(cast(I), LLVMContext::MD_nonnull)) |
| 2688 |
return true; |
2688 |
return true; |
| 2689 |
|
2689 |
|
| 2690 |
// No need to fall through to computeKnownBits as range metadata is already |
2690 |
// No need to fall through to computeKnownBits as range metadata is already |
| 2691 |
// handled in isKnownNonZero. |
2691 |
// handled in isKnownNonZero. |
| 2692 |
return false; |
2692 |
return false; |
| 2693 |
case Instruction::Call: |
2693 |
case Instruction::Call: |
| 2694 |
case Instruction::Invoke: |
2694 |
case Instruction::Invoke: |
| 2695 |
if (I->getType()->isPointerTy()) { |
2695 |
if (I->getType()->isPointerTy()) { |
| 2696 |
const auto *Call = cast(I); |
2696 |
const auto *Call = cast(I); |
| 2697 |
if (Call->isReturnNonNull()) |
2697 |
if (Call->isReturnNonNull()) |
| 2698 |
return true; |
2698 |
return true; |
| 2699 |
if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true)) |
2699 |
if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true)) |
| 2700 |
return isKnownNonZero(RP, Depth, Q); |
2700 |
return isKnownNonZero(RP, Depth, Q); |
| 2701 |
} |
2701 |
} |
| 2702 |
|
2702 |
|
| 2703 |
if (auto *II = dyn_cast(I)) { |
2703 |
if (auto *II = dyn_cast(I)) { |
| 2704 |
switch (II->getIntrinsicID()) { |
2704 |
switch (II->getIntrinsicID()) { |
| 2705 |
case Intrinsic::sshl_sat: |
2705 |
case Intrinsic::sshl_sat: |
| 2706 |
case Intrinsic::ushl_sat: |
2706 |
case Intrinsic::ushl_sat: |
| 2707 |
case Intrinsic::abs: |
2707 |
case Intrinsic::abs: |
| 2708 |
case Intrinsic::bitreverse: |
2708 |
case Intrinsic::bitreverse: |
| 2709 |
case Intrinsic::bswap: |
2709 |
case Intrinsic::bswap: |
| 2710 |
case Intrinsic::ctpop: |
2710 |
case Intrinsic::ctpop: |
| 2711 |
return isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q); |
2711 |
return isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q); |
| 2712 |
case Intrinsic::ssub_sat: |
2712 |
case Intrinsic::ssub_sat: |
| 2713 |
return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, |
2713 |
return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, |
| 2714 |
II->getArgOperand(0), II->getArgOperand(1)); |
2714 |
II->getArgOperand(0), II->getArgOperand(1)); |
| 2715 |
case Intrinsic::sadd_sat: |
2715 |
case Intrinsic::sadd_sat: |
| 2716 |
return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, |
2716 |
return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, |
| 2717 |
II->getArgOperand(0), II->getArgOperand(1), |
2717 |
II->getArgOperand(0), II->getArgOperand(1), |
| 2718 |
/*NSW*/ true); |
2718 |
/*NSW*/ true); |
| 2719 |
case Intrinsic::umax: |
2719 |
case Intrinsic::umax: |
| 2720 |
case Intrinsic::uadd_sat: |
2720 |
case Intrinsic::uadd_sat: |
| 2721 |
return isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q) || |
2721 |
return isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q) || |
| 2722 |
isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q); |
2722 |
isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q); |
| 2723 |
case Intrinsic::smin: |
2723 |
case Intrinsic::smin: |
| 2724 |
case Intrinsic::smax: { |
2724 |
case Intrinsic::smax: { |
| 2725 |
auto KnownOpImpliesNonZero = [&](const KnownBits &K) { |
2725 |
auto KnownOpImpliesNonZero = [&](const KnownBits &K) { |
| 2726 |
return II->getIntrinsicID() == Intrinsic::smin |
2726 |
return II->getIntrinsicID() == Intrinsic::smin |
| 2727 |
? K.isNegative() |
2727 |
? K.isNegative() |
| 2728 |
: K.isStrictlyPositive(); |
2728 |
: K.isStrictlyPositive(); |
| 2729 |
}; |
2729 |
}; |
| 2730 |
KnownBits XKnown = |
2730 |
KnownBits XKnown = |
| 2731 |
computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q); |
2731 |
computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q); |
| 2732 |
if (KnownOpImpliesNonZero(XKnown)) |
2732 |
if (KnownOpImpliesNonZero(XKnown)) |
| 2733 |
return true; |
2733 |
return true; |
| 2734 |
KnownBits YKnown = |
2734 |
KnownBits YKnown = |
| 2735 |
computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q); |
2735 |
computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q); |
| 2736 |
if (KnownOpImpliesNonZero(YKnown)) |
2736 |
if (KnownOpImpliesNonZero(YKnown)) |
| 2737 |
return true; |
2737 |
return true; |
| 2738 |
|
2738 |
|
| 2739 |
if (XKnown.isNonZero() && YKnown.isNonZero()) |
2739 |
if (XKnown.isNonZero() && YKnown.isNonZero()) |
| 2740 |
return true; |
2740 |
return true; |
| 2741 |
} |
2741 |
} |
| 2742 |
[[fallthrough]]; |
2742 |
[[fallthrough]]; |
| 2743 |
case Intrinsic::umin: |
2743 |
case Intrinsic::umin: |
| 2744 |
return isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q) && |
2744 |
return isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q) && |
| 2745 |
isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q); |
2745 |
isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q); |
| 2746 |
case Intrinsic::cttz: |
2746 |
case Intrinsic::cttz: |
| 2747 |
return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q) |
2747 |
return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q) |
| 2748 |
.Zero[0]; |
2748 |
.Zero[0]; |
| 2749 |
case Intrinsic::ctlz: |
2749 |
case Intrinsic::ctlz: |
| 2750 |
return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q) |
2750 |
return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q) |
| 2751 |
.isNonNegative(); |
2751 |
.isNonNegative(); |
| 2752 |
case Intrinsic::fshr: |
2752 |
case Intrinsic::fshr: |
| 2753 |
case Intrinsic::fshl: |
2753 |
case Intrinsic::fshl: |
| 2754 |
// If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0. |
2754 |
// If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0. |
| 2755 |
if (II->getArgOperand(0) == II->getArgOperand(1)) |
2755 |
if (II->getArgOperand(0) == II->getArgOperand(1)) |
| 2756 |
return isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q); |
2756 |
return isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q); |
| 2757 |
break; |
2757 |
break; |
| 2758 |
case Intrinsic::vscale: |
2758 |
case Intrinsic::vscale: |
| 2759 |
return true; |
2759 |
return true; |
| 2760 |
default: |
2760 |
default: |
| 2761 |
break; |
2761 |
break; |
| 2762 |
} |
2762 |
} |
| 2763 |
} |
2763 |
} |
| 2764 |
break; |
2764 |
break; |
| 2765 |
} |
2765 |
} |
| 2766 |
|
2766 |
|
| 2767 |
KnownBits Known(BitWidth); |
2767 |
KnownBits Known(BitWidth); |
| 2768 |
computeKnownBits(I, DemandedElts, Known, Depth, Q); |
2768 |
computeKnownBits(I, DemandedElts, Known, Depth, Q); |
| 2769 |
return Known.One != 0; |
2769 |
return Known.One != 0; |
| 2770 |
} |
2770 |
} |
| 2771 |
|
2771 |
|
| 2772 |
/// Return true if the given value is known to be non-zero when defined. For |
2772 |
/// Return true if the given value is known to be non-zero when defined. For |
| 2773 |
/// vectors, return true if every demanded element is known to be non-zero when |
2773 |
/// vectors, return true if every demanded element is known to be non-zero when |
| 2774 |
/// defined. For pointers, if the context instruction and dominator tree are |
2774 |
/// defined. For pointers, if the context instruction and dominator tree are |
| 2775 |
/// specified, perform context-sensitive analysis and return true if the |
2775 |
/// specified, perform context-sensitive analysis and return true if the |
| 2776 |
/// pointer couldn't possibly be null at the specified instruction. |
2776 |
/// pointer couldn't possibly be null at the specified instruction. |
| 2777 |
/// Supports values with integer or pointer type and vectors of integers. |
2777 |
/// Supports values with integer or pointer type and vectors of integers. |
| 2778 |
bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth, |
2778 |
bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth, |
| 2779 |
const SimplifyQuery &Q) { |
2779 |
const SimplifyQuery &Q) { |
| 2780 |
|
2780 |
|
| 2781 |
#ifndef NDEBUG |
2781 |
#ifndef NDEBUG |
| 2782 |
Type *Ty = V->getType(); |
2782 |
Type *Ty = V->getType(); |
| 2783 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
2783 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
| 2784 |
|
2784 |
|
| 2785 |
if (auto *FVTy = dyn_cast(Ty)) { |
2785 |
if (auto *FVTy = dyn_cast(Ty)) { |
| 2786 |
assert( |
2786 |
assert( |
| 2787 |
FVTy->getNumElements() == DemandedElts.getBitWidth() && |
2787 |
FVTy->getNumElements() == DemandedElts.getBitWidth() && |
| 2788 |
"DemandedElt width should equal the fixed vector number of elements"); |
2788 |
"DemandedElt width should equal the fixed vector number of elements"); |
| 2789 |
} else { |
2789 |
} else { |
| 2790 |
assert(DemandedElts == APInt(1, 1) && |
2790 |
assert(DemandedElts == APInt(1, 1) && |
| 2791 |
"DemandedElt width should be 1 for scalars"); |
2791 |
"DemandedElt width should be 1 for scalars"); |
| 2792 |
} |
2792 |
} |
| 2793 |
#endif |
2793 |
#endif |
| 2794 |
|
2794 |
|
| 2795 |
if (auto *C = dyn_cast(V)) { |
2795 |
if (auto *C = dyn_cast(V)) { |
| 2796 |
if (C->isNullValue()) |
2796 |
if (C->isNullValue()) |
| 2797 |
return false; |
2797 |
return false; |
| 2798 |
if (isa(C)) |
2798 |
if (isa(C)) |
| 2799 |
// Must be non-zero due to null test above. |
2799 |
// Must be non-zero due to null test above. |
| 2800 |
return true; |
2800 |
return true; |
| 2801 |
|
2801 |
|
| 2802 |
// For constant vectors, check that all elements are undefined or known |
2802 |
// For constant vectors, check that all elements are undefined or known |
| 2803 |
// non-zero to determine that the whole vector is known non-zero. |
2803 |
// non-zero to determine that the whole vector is known non-zero. |
| 2804 |
if (auto *VecTy = dyn_cast(C->getType())) { |
2804 |
if (auto *VecTy = dyn_cast(C->getType())) { |
| 2805 |
for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) { |
2805 |
for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) { |
| 2806 |
if (!DemandedElts[i]) |
2806 |
if (!DemandedElts[i]) |
| 2807 |
continue; |
2807 |
continue; |
| 2808 |
Constant *Elt = C->getAggregateElement(i); |
2808 |
Constant *Elt = C->getAggregateElement(i); |
| 2809 |
if (!Elt || Elt->isNullValue()) |
2809 |
if (!Elt || Elt->isNullValue()) |
| 2810 |
return false; |
2810 |
return false; |
| 2811 |
if (!isa(Elt) && !isa(Elt)) |
2811 |
if (!isa(Elt) && !isa(Elt)) |
| 2812 |
return false; |
2812 |
return false; |
| 2813 |
} |
2813 |
} |
| 2814 |
return true; |
2814 |
return true; |
| 2815 |
} |
2815 |
} |
| 2816 |
|
2816 |
|
| 2817 |
// A global variable in address space 0 is non null unless extern weak |
2817 |
// A global variable in address space 0 is non null unless extern weak |
| 2818 |
// or an absolute symbol reference. Other address spaces may have null as a |
2818 |
// or an absolute symbol reference. Other address spaces may have null as a |
| 2819 |
// valid address for a global, so we can't assume anything. |
2819 |
// valid address for a global, so we can't assume anything. |
| 2820 |
if (const GlobalValue *GV = dyn_cast(V)) { |
2820 |
if (const GlobalValue *GV = dyn_cast(V)) { |
| 2821 |
if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() && |
2821 |
if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() && |
| 2822 |
GV->getType()->getAddressSpace() == 0) |
2822 |
GV->getType()->getAddressSpace() == 0) |
| 2823 |
return true; |
2823 |
return true; |
| 2824 |
} |
2824 |
} |
| 2825 |
|
2825 |
|
| 2826 |
// For constant expressions, fall through to the Operator code below. |
2826 |
// For constant expressions, fall through to the Operator code below. |
| 2827 |
if (!isa(V)) |
2827 |
if (!isa(V)) |
| 2828 |
return false; |
2828 |
return false; |
| 2829 |
} |
2829 |
} |
| 2830 |
|
2830 |
|
| 2831 |
if (auto *I = dyn_cast(V)) { |
2831 |
if (auto *I = dyn_cast(V)) { |
| 2832 |
if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) { |
2832 |
if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) { |
| 2833 |
// If the possible ranges don't contain zero, then the value is |
2833 |
// If the possible ranges don't contain zero, then the value is |
| 2834 |
// definitely non-zero. |
2834 |
// definitely non-zero. |
| 2835 |
if (auto *Ty = dyn_cast(V->getType())) { |
2835 |
if (auto *Ty = dyn_cast(V->getType())) { |
| 2836 |
const APInt ZeroValue(Ty->getBitWidth(), 0); |
2836 |
const APInt ZeroValue(Ty->getBitWidth(), 0); |
| 2837 |
if (rangeMetadataExcludesValue(Ranges, ZeroValue)) |
2837 |
if (rangeMetadataExcludesValue(Ranges, ZeroValue)) |
| 2838 |
return true; |
2838 |
return true; |
| 2839 |
} |
2839 |
} |
| 2840 |
} |
2840 |
} |
| 2841 |
} |
2841 |
} |
| 2842 |
|
2842 |
|
| 2843 |
if (!isa(V) && isKnownNonZeroFromAssume(V, Q)) |
2843 |
if (!isa(V) && isKnownNonZeroFromAssume(V, Q)) |
| 2844 |
return true; |
2844 |
return true; |
| 2845 |
|
2845 |
|
| 2846 |
// Some of the tests below are recursive, so bail out if we hit the limit. |
2846 |
// Some of the tests below are recursive, so bail out if we hit the limit. |
| 2847 |
if (Depth++ >= MaxAnalysisRecursionDepth) |
2847 |
if (Depth++ >= MaxAnalysisRecursionDepth) |
| 2848 |
return false; |
2848 |
return false; |
| 2849 |
|
2849 |
|
| 2850 |
// Check for pointer simplifications. |
2850 |
// Check for pointer simplifications. |
| 2851 |
|
2851 |
|
| 2852 |
if (PointerType *PtrTy = dyn_cast(V->getType())) { |
2852 |
if (PointerType *PtrTy = dyn_cast(V->getType())) { |
| 2853 |
// A byval, inalloca may not be null in a non-default addres space. A |
2853 |
// A byval, inalloca may not be null in a non-default addres space. A |
| 2854 |
// nonnull argument is assumed never 0. |
2854 |
// nonnull argument is assumed never 0. |
| 2855 |
if (const Argument *A = dyn_cast(V)) { |
2855 |
if (const Argument *A = dyn_cast(V)) { |
| 2856 |
if (((A->hasPassPointeeByValueCopyAttr() && |
2856 |
if (((A->hasPassPointeeByValueCopyAttr() && |
| 2857 |
!NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) || |
2857 |
!NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) || |
| 2858 |
A->hasNonNullAttr())) |
2858 |
A->hasNonNullAttr())) |
| 2859 |
return true; |
2859 |
return true; |
| 2860 |
} |
2860 |
} |
| 2861 |
} |
2861 |
} |
| 2862 |
|
2862 |
|
| 2863 |
if (const auto *I = dyn_cast(V)) |
2863 |
if (const auto *I = dyn_cast(V)) |
| 2864 |
if (isKnownNonZeroFromOperator(I, DemandedElts, Depth, Q)) |
2864 |
if (isKnownNonZeroFromOperator(I, DemandedElts, Depth, Q)) |
| 2865 |
return true; |
2865 |
return true; |
| 2866 |
|
2866 |
|
| 2867 |
if (!isa(V) && |
2867 |
if (!isa(V) && |
| 2868 |
isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT)) |
2868 |
isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT)) |
| 2869 |
return true; |
2869 |
return true; |
| 2870 |
|
2870 |
|
| 2871 |
return false; |
2871 |
return false; |
| 2872 |
} |
2872 |
} |
| 2873 |
|
2873 |
|
| 2874 |
bool isKnownNonZero(const Value *V, unsigned Depth, const SimplifyQuery &Q) { |
2874 |
bool isKnownNonZero(const Value *V, unsigned Depth, const SimplifyQuery &Q) { |
| 2875 |
auto *FVTy = dyn_cast(V->getType()); |
2875 |
auto *FVTy = dyn_cast(V->getType()); |
| 2876 |
APInt DemandedElts = |
2876 |
APInt DemandedElts = |
| 2877 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
2877 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
| 2878 |
return isKnownNonZero(V, DemandedElts, Depth, Q); |
2878 |
return isKnownNonZero(V, DemandedElts, Depth, Q); |
| 2879 |
} |
2879 |
} |
| 2880 |
|
2880 |
|
| 2881 |
/// If the pair of operators are the same invertible function, return the |
2881 |
/// If the pair of operators are the same invertible function, return the |
| 2882 |
/// the operands of the function corresponding to each input. Otherwise, |
2882 |
/// the operands of the function corresponding to each input. Otherwise, |
| 2883 |
/// return std::nullopt. An invertible function is one that is 1-to-1 and maps |
2883 |
/// return std::nullopt. An invertible function is one that is 1-to-1 and maps |
| 2884 |
/// every input value to exactly one output value. This is equivalent to |
2884 |
/// every input value to exactly one output value. This is equivalent to |
| 2885 |
/// saying that Op1 and Op2 are equal exactly when the specified pair of |
2885 |
/// saying that Op1 and Op2 are equal exactly when the specified pair of |
| 2886 |
/// operands are equal, (except that Op1 and Op2 may be poison more often.) |
2886 |
/// operands are equal, (except that Op1 and Op2 may be poison more often.) |
| 2887 |
static std::optional> |
2887 |
static std::optional> |
| 2888 |
getInvertibleOperands(const Operator *Op1, |
2888 |
getInvertibleOperands(const Operator *Op1, |
| 2889 |
const Operator *Op2) { |
2889 |
const Operator *Op2) { |
| 2890 |
if (Op1->getOpcode() != Op2->getOpcode()) |
2890 |
if (Op1->getOpcode() != Op2->getOpcode()) |
| 2891 |
return std::nullopt; |
2891 |
return std::nullopt; |
| 2892 |
|
2892 |
|
| 2893 |
auto getOperands = [&](unsigned OpNum) -> auto { |
2893 |
auto getOperands = [&](unsigned OpNum) -> auto { |
| 2894 |
return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum)); |
2894 |
return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum)); |
| 2895 |
}; |
2895 |
}; |
| 2896 |
|
2896 |
|
| 2897 |
switch (Op1->getOpcode()) { |
2897 |
switch (Op1->getOpcode()) { |
| 2898 |
default: |
2898 |
default: |
| 2899 |
break; |
2899 |
break; |
| 2900 |
case Instruction::Add: |
2900 |
case Instruction::Add: |
| 2901 |
case Instruction::Sub: |
2901 |
case Instruction::Sub: |
| 2902 |
if (Op1->getOperand(0) == Op2->getOperand(0)) |
2902 |
if (Op1->getOperand(0) == Op2->getOperand(0)) |
| 2903 |
return getOperands(1); |
2903 |
return getOperands(1); |
| 2904 |
if (Op1->getOperand(1) == Op2->getOperand(1)) |
2904 |
if (Op1->getOperand(1) == Op2->getOperand(1)) |
| 2905 |
return getOperands(0); |
2905 |
return getOperands(0); |
| 2906 |
break; |
2906 |
break; |
| 2907 |
case Instruction::Mul: { |
2907 |
case Instruction::Mul: { |
| 2908 |
// invertible if A * B == (A * B) mod 2^N where A, and B are integers |
2908 |
// invertible if A * B == (A * B) mod 2^N where A, and B are integers |
| 2909 |
// and N is the bitwdith. The nsw case is non-obvious, but proven by |
2909 |
// and N is the bitwdith. The nsw case is non-obvious, but proven by |
| 2910 |
// alive2: https://alive2.llvm.org/ce/z/Z6D5qK |
2910 |
// alive2: https://alive2.llvm.org/ce/z/Z6D5qK |
| 2911 |
auto *OBO1 = cast(Op1); |
2911 |
auto *OBO1 = cast(Op1); |
| 2912 |
auto *OBO2 = cast(Op2); |
2912 |
auto *OBO2 = cast(Op2); |
| 2913 |
if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) && |
2913 |
if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) && |
| 2914 |
(!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap())) |
2914 |
(!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap())) |
| 2915 |
break; |
2915 |
break; |
| 2916 |
|
2916 |
|
| 2917 |
// Assume operand order has been canonicalized |
2917 |
// Assume operand order has been canonicalized |
| 2918 |
if (Op1->getOperand(1) == Op2->getOperand(1) && |
2918 |
if (Op1->getOperand(1) == Op2->getOperand(1) && |
| 2919 |
isa(Op1->getOperand(1)) && |
2919 |
isa(Op1->getOperand(1)) && |
| 2920 |
!cast(Op1->getOperand(1))->isZero()) |
2920 |
!cast(Op1->getOperand(1))->isZero()) |
| 2921 |
return getOperands(0); |
2921 |
return getOperands(0); |
| 2922 |
break; |
2922 |
break; |
| 2923 |
} |
2923 |
} |
| 2924 |
case Instruction::Shl: { |
2924 |
case Instruction::Shl: { |
| 2925 |
// Same as multiplies, with the difference that we don't need to check |
2925 |
// Same as multiplies, with the difference that we don't need to check |
| 2926 |
// for a non-zero multiply. Shifts always multiply by non-zero. |
2926 |
// for a non-zero multiply. Shifts always multiply by non-zero. |
| 2927 |
auto *OBO1 = cast(Op1); |
2927 |
auto *OBO1 = cast(Op1); |
| 2928 |
auto *OBO2 = cast(Op2); |
2928 |
auto *OBO2 = cast(Op2); |
| 2929 |
if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) && |
2929 |
if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) && |
| 2930 |
(!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap())) |
2930 |
(!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap())) |
| 2931 |
break; |
2931 |
break; |
| 2932 |
|
2932 |
|
| 2933 |
if (Op1->getOperand(1) == Op2->getOperand(1)) |
2933 |
if (Op1->getOperand(1) == Op2->getOperand(1)) |
| 2934 |
return getOperands(0); |
2934 |
return getOperands(0); |
| 2935 |
break; |
2935 |
break; |
| 2936 |
} |
2936 |
} |
| 2937 |
case Instruction::AShr: |
2937 |
case Instruction::AShr: |
| 2938 |
case Instruction::LShr: { |
2938 |
case Instruction::LShr: { |
| 2939 |
auto *PEO1 = cast(Op1); |
2939 |
auto *PEO1 = cast(Op1); |
| 2940 |
auto *PEO2 = cast(Op2); |
2940 |
auto *PEO2 = cast(Op2); |
| 2941 |
if (!PEO1->isExact() || !PEO2->isExact()) |
2941 |
if (!PEO1->isExact() || !PEO2->isExact()) |
| 2942 |
break; |
2942 |
break; |
| 2943 |
|
2943 |
|
| 2944 |
if (Op1->getOperand(1) == Op2->getOperand(1)) |
2944 |
if (Op1->getOperand(1) == Op2->getOperand(1)) |
| 2945 |
return getOperands(0); |
2945 |
return getOperands(0); |
| 2946 |
break; |
2946 |
break; |
| 2947 |
} |
2947 |
} |
| 2948 |
case Instruction::SExt: |
2948 |
case Instruction::SExt: |
| 2949 |
case Instruction::ZExt: |
2949 |
case Instruction::ZExt: |
| 2950 |
if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType()) |
2950 |
if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType()) |
| 2951 |
return getOperands(0); |
2951 |
return getOperands(0); |
| 2952 |
break; |
2952 |
break; |
| 2953 |
case Instruction::PHI: { |
2953 |
case Instruction::PHI: { |
| 2954 |
const PHINode *PN1 = cast(Op1); |
2954 |
const PHINode *PN1 = cast(Op1); |
| 2955 |
const PHINode *PN2 = cast(Op2); |
2955 |
const PHINode *PN2 = cast(Op2); |
| 2956 |
|
2956 |
|
| 2957 |
// If PN1 and PN2 are both recurrences, can we prove the entire recurrences |
2957 |
// If PN1 and PN2 are both recurrences, can we prove the entire recurrences |
| 2958 |
// are a single invertible function of the start values? Note that repeated |
2958 |
// are a single invertible function of the start values? Note that repeated |
| 2959 |
// application of an invertible function is also invertible |
2959 |
// application of an invertible function is also invertible |
| 2960 |
BinaryOperator *BO1 = nullptr; |
2960 |
BinaryOperator *BO1 = nullptr; |
| 2961 |
Value *Start1 = nullptr, *Step1 = nullptr; |
2961 |
Value *Start1 = nullptr, *Step1 = nullptr; |
| 2962 |
BinaryOperator *BO2 = nullptr; |
2962 |
BinaryOperator *BO2 = nullptr; |
| 2963 |
Value *Start2 = nullptr, *Step2 = nullptr; |
2963 |
Value *Start2 = nullptr, *Step2 = nullptr; |
| 2964 |
if (PN1->getParent() != PN2->getParent() || |
2964 |
if (PN1->getParent() != PN2->getParent() || |
| 2965 |
!matchSimpleRecurrence(PN1, BO1, Start1, Step1) || |
2965 |
!matchSimpleRecurrence(PN1, BO1, Start1, Step1) || |
| 2966 |
!matchSimpleRecurrence(PN2, BO2, Start2, Step2)) |
2966 |
!matchSimpleRecurrence(PN2, BO2, Start2, Step2)) |
| 2967 |
break; |
2967 |
break; |
| 2968 |
|
2968 |
|
| 2969 |
auto Values = getInvertibleOperands(cast(BO1), |
2969 |
auto Values = getInvertibleOperands(cast(BO1), |
| 2970 |
cast(BO2)); |
2970 |
cast(BO2)); |
| 2971 |
if (!Values) |
2971 |
if (!Values) |
| 2972 |
break; |
2972 |
break; |
| 2973 |
|
2973 |
|
| 2974 |
// We have to be careful of mutually defined recurrences here. Ex: |
2974 |
// We have to be careful of mutually defined recurrences here. Ex: |
| 2975 |
// * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V |
2975 |
// * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V |
| 2976 |
// * X_i = Y_i = X_(i-1) OP Y_(i-1) |
2976 |
// * X_i = Y_i = X_(i-1) OP Y_(i-1) |
| 2977 |
// The invertibility of these is complicated, and not worth reasoning |
2977 |
// The invertibility of these is complicated, and not worth reasoning |
| 2978 |
// about (yet?). |
2978 |
// about (yet?). |
| 2979 |
if (Values->first != PN1 || Values->second != PN2) |
2979 |
if (Values->first != PN1 || Values->second != PN2) |
| 2980 |
break; |
2980 |
break; |
| 2981 |
|
2981 |
|
| 2982 |
return std::make_pair(Start1, Start2); |
2982 |
return std::make_pair(Start1, Start2); |
| 2983 |
} |
2983 |
} |
| 2984 |
} |
2984 |
} |
| 2985 |
return std::nullopt; |
2985 |
return std::nullopt; |
| 2986 |
} |
2986 |
} |
| 2987 |
|
2987 |
|
| 2988 |
/// Return true if V2 == V1 + X, where X is known non-zero. |
2988 |
/// Return true if V2 == V1 + X, where X is known non-zero. |
| 2989 |
static bool isAddOfNonZero(const Value *V1, const Value *V2, unsigned Depth, |
2989 |
static bool isAddOfNonZero(const Value *V1, const Value *V2, unsigned Depth, |
| 2990 |
const SimplifyQuery &Q) { |
2990 |
const SimplifyQuery &Q) { |
| 2991 |
const BinaryOperator *BO = dyn_cast(V1); |
2991 |
const BinaryOperator *BO = dyn_cast(V1); |
| 2992 |
if (!BO || BO->getOpcode() != Instruction::Add) |
2992 |
if (!BO || BO->getOpcode() != Instruction::Add) |
| 2993 |
return false; |
2993 |
return false; |
| 2994 |
Value *Op = nullptr; |
2994 |
Value *Op = nullptr; |
| 2995 |
if (V2 == BO->getOperand(0)) |
2995 |
if (V2 == BO->getOperand(0)) |
| 2996 |
Op = BO->getOperand(1); |
2996 |
Op = BO->getOperand(1); |
| 2997 |
else if (V2 == BO->getOperand(1)) |
2997 |
else if (V2 == BO->getOperand(1)) |
| 2998 |
Op = BO->getOperand(0); |
2998 |
Op = BO->getOperand(0); |
| 2999 |
else |
2999 |
else |
| 3000 |
return false; |
3000 |
return false; |
| 3001 |
return isKnownNonZero(Op, Depth + 1, Q); |
3001 |
return isKnownNonZero(Op, Depth + 1, Q); |
| 3002 |
} |
3002 |
} |
| 3003 |
|
3003 |
|
| 3004 |
/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and |
3004 |
/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and |
| 3005 |
/// the multiplication is nuw or nsw. |
3005 |
/// the multiplication is nuw or nsw. |
| 3006 |
static bool isNonEqualMul(const Value *V1, const Value *V2, unsigned Depth, |
3006 |
static bool isNonEqualMul(const Value *V1, const Value *V2, unsigned Depth, |
| 3007 |
const SimplifyQuery &Q) { |
3007 |
const SimplifyQuery &Q) { |
| 3008 |
if (auto *OBO = dyn_cast(V2)) { |
3008 |
if (auto *OBO = dyn_cast(V2)) { |
| 3009 |
const APInt *C; |
3009 |
const APInt *C; |
| 3010 |
return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) && |
3010 |
return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) && |
| 3011 |
(OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) && |
3011 |
(OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) && |
| 3012 |
!C->isZero() && !C->isOne() && isKnownNonZero(V1, Depth + 1, Q); |
3012 |
!C->isZero() && !C->isOne() && isKnownNonZero(V1, Depth + 1, Q); |
| 3013 |
} |
3013 |
} |
| 3014 |
return false; |
3014 |
return false; |
| 3015 |
} |
3015 |
} |
| 3016 |
|
3016 |
|
| 3017 |
/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and |
3017 |
/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and |
| 3018 |
/// the shift is nuw or nsw. |
3018 |
/// the shift is nuw or nsw. |
| 3019 |
static bool isNonEqualShl(const Value *V1, const Value *V2, unsigned Depth, |
3019 |
static bool isNonEqualShl(const Value *V1, const Value *V2, unsigned Depth, |
| 3020 |
const SimplifyQuery &Q) { |
3020 |
const SimplifyQuery &Q) { |
| 3021 |
if (auto *OBO = dyn_cast(V2)) { |
3021 |
if (auto *OBO = dyn_cast(V2)) { |
| 3022 |
const APInt *C; |
3022 |
const APInt *C; |
| 3023 |
return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) && |
3023 |
return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) && |
| 3024 |
(OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) && |
3024 |
(OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) && |
| 3025 |
!C->isZero() && isKnownNonZero(V1, Depth + 1, Q); |
3025 |
!C->isZero() && isKnownNonZero(V1, Depth + 1, Q); |
| 3026 |
} |
3026 |
} |
| 3027 |
return false; |
3027 |
return false; |
| 3028 |
} |
3028 |
} |
| 3029 |
|
3029 |
|
| 3030 |
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, |
3030 |
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, |
| 3031 |
unsigned Depth, const SimplifyQuery &Q) { |
3031 |
unsigned Depth, const SimplifyQuery &Q) { |
| 3032 |
// Check two PHIs are in same block. |
3032 |
// Check two PHIs are in same block. |
| 3033 |
if (PN1->getParent() != PN2->getParent()) |
3033 |
if (PN1->getParent() != PN2->getParent()) |
| 3034 |
return false; |
3034 |
return false; |
| 3035 |
|
3035 |
|
| 3036 |
SmallPtrSet VisitedBBs; |
3036 |
SmallPtrSet VisitedBBs; |
| 3037 |
bool UsedFullRecursion = false; |
3037 |
bool UsedFullRecursion = false; |
| 3038 |
for (const BasicBlock *IncomBB : PN1->blocks()) { |
3038 |
for (const BasicBlock *IncomBB : PN1->blocks()) { |
| 3039 |
if (!VisitedBBs.insert(IncomBB).second) |
3039 |
if (!VisitedBBs.insert(IncomBB).second) |
| 3040 |
continue; // Don't reprocess blocks that we have dealt with already. |
3040 |
continue; // Don't reprocess blocks that we have dealt with already. |
| 3041 |
const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB); |
3041 |
const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB); |
| 3042 |
const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB); |
3042 |
const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB); |
| 3043 |
const APInt *C1, *C2; |
3043 |
const APInt *C1, *C2; |
| 3044 |
if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2) |
3044 |
if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2) |
| 3045 |
continue; |
3045 |
continue; |
| 3046 |
|
3046 |
|
| 3047 |
// Only one pair of phi operands is allowed for full recursion. |
3047 |
// Only one pair of phi operands is allowed for full recursion. |
| 3048 |
if (UsedFullRecursion) |
3048 |
if (UsedFullRecursion) |
| 3049 |
return false; |
3049 |
return false; |
| 3050 |
|
3050 |
|
| 3051 |
SimplifyQuery RecQ = Q; |
3051 |
SimplifyQuery RecQ = Q; |
| 3052 |
RecQ.CxtI = IncomBB->getTerminator(); |
3052 |
RecQ.CxtI = IncomBB->getTerminator(); |
| 3053 |
if (!isKnownNonEqual(IV1, IV2, Depth + 1, RecQ)) |
3053 |
if (!isKnownNonEqual(IV1, IV2, Depth + 1, RecQ)) |
| 3054 |
return false; |
3054 |
return false; |
| 3055 |
UsedFullRecursion = true; |
3055 |
UsedFullRecursion = true; |
| 3056 |
} |
3056 |
} |
| 3057 |
return true; |
3057 |
return true; |
| 3058 |
} |
3058 |
} |
| 3059 |
|
3059 |
|
| 3060 |
/// Return true if it is known that V1 != V2. |
3060 |
/// Return true if it is known that V1 != V2. |
| 3061 |
static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth, |
3061 |
static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth, |
| 3062 |
const SimplifyQuery &Q) { |
3062 |
const SimplifyQuery &Q) { |
| 3063 |
if (V1 == V2) |
3063 |
if (V1 == V2) |
| 3064 |
return false; |
3064 |
return false; |
| 3065 |
if (V1->getType() != V2->getType()) |
3065 |
if (V1->getType() != V2->getType()) |
| 3066 |
// We can't look through casts yet. |
3066 |
// We can't look through casts yet. |
| 3067 |
return false; |
3067 |
return false; |
| 3068 |
|
3068 |
|
| 3069 |
if (Depth >= MaxAnalysisRecursionDepth) |
3069 |
if (Depth >= MaxAnalysisRecursionDepth) |
| 3070 |
return false; |
3070 |
return false; |
| 3071 |
|
3071 |
|
| 3072 |
// See if we can recurse through (exactly one of) our operands. This |
3072 |
// See if we can recurse through (exactly one of) our operands. This |
| 3073 |
// requires our operation be 1-to-1 and map every input value to exactly |
3073 |
// requires our operation be 1-to-1 and map every input value to exactly |
| 3074 |
// one output value. Such an operation is invertible. |
3074 |
// one output value. Such an operation is invertible. |
| 3075 |
auto *O1 = dyn_cast(V1); |
3075 |
auto *O1 = dyn_cast(V1); |
| 3076 |
auto *O2 = dyn_cast(V2); |
3076 |
auto *O2 = dyn_cast(V2); |
| 3077 |
if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) { |
3077 |
if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) { |
| 3078 |
if (auto Values = getInvertibleOperands(O1, O2)) |
3078 |
if (auto Values = getInvertibleOperands(O1, O2)) |
| 3079 |
return isKnownNonEqual(Values->first, Values->second, Depth + 1, Q); |
3079 |
return isKnownNonEqual(Values->first, Values->second, Depth + 1, Q); |
| 3080 |
|
3080 |
|
| 3081 |
if (const PHINode *PN1 = dyn_cast(V1)) { |
3081 |
if (const PHINode *PN1 = dyn_cast(V1)) { |
| 3082 |
const PHINode *PN2 = cast(V2); |
3082 |
const PHINode *PN2 = cast(V2); |
| 3083 |
// FIXME: This is missing a generalization to handle the case where one is |
3083 |
// FIXME: This is missing a generalization to handle the case where one is |
| 3084 |
// a PHI and another one isn't. |
3084 |
// a PHI and another one isn't. |
| 3085 |
if (isNonEqualPHIs(PN1, PN2, Depth, Q)) |
3085 |
if (isNonEqualPHIs(PN1, PN2, Depth, Q)) |
| 3086 |
return true; |
3086 |
return true; |
| 3087 |
}; |
3087 |
}; |
| 3088 |
} |
3088 |
} |
| 3089 |
|
3089 |
|
| 3090 |
if (isAddOfNonZero(V1, V2, Depth, Q) || isAddOfNonZero(V2, V1, Depth, Q)) |
3090 |
if (isAddOfNonZero(V1, V2, Depth, Q) || isAddOfNonZero(V2, V1, Depth, Q)) |
| 3091 |
return true; |
3091 |
return true; |
| 3092 |
|
3092 |
|
| 3093 |
if (isNonEqualMul(V1, V2, Depth, Q) || isNonEqualMul(V2, V1, Depth, Q)) |
3093 |
if (isNonEqualMul(V1, V2, Depth, Q) || isNonEqualMul(V2, V1, Depth, Q)) |
| 3094 |
return true; |
3094 |
return true; |
| 3095 |
|
3095 |
|
| 3096 |
if (isNonEqualShl(V1, V2, Depth, Q) || isNonEqualShl(V2, V1, Depth, Q)) |
3096 |
if (isNonEqualShl(V1, V2, Depth, Q) || isNonEqualShl(V2, V1, Depth, Q)) |
| 3097 |
return true; |
3097 |
return true; |
| 3098 |
|
3098 |
|
| 3099 |
if (V1->getType()->isIntOrIntVectorTy()) { |
3099 |
if (V1->getType()->isIntOrIntVectorTy()) { |
| 3100 |
// Are any known bits in V1 contradictory to known bits in V2? If V1 |
3100 |
// Are any known bits in V1 contradictory to known bits in V2? If V1 |
| 3101 |
// has a known zero where V2 has a known one, they must not be equal. |
3101 |
// has a known zero where V2 has a known one, they must not be equal. |
| 3102 |
KnownBits Known1 = computeKnownBits(V1, Depth, Q); |
3102 |
KnownBits Known1 = computeKnownBits(V1, Depth, Q); |
| 3103 |
KnownBits Known2 = computeKnownBits(V2, Depth, Q); |
3103 |
KnownBits Known2 = computeKnownBits(V2, Depth, Q); |
| 3104 |
|
3104 |
|
| 3105 |
if (Known1.Zero.intersects(Known2.One) || |
3105 |
if (Known1.Zero.intersects(Known2.One) || |
| 3106 |
Known2.Zero.intersects(Known1.One)) |
3106 |
Known2.Zero.intersects(Known1.One)) |
| 3107 |
return true; |
3107 |
return true; |
| 3108 |
} |
3108 |
} |
| 3109 |
return false; |
3109 |
return false; |
| 3110 |
} |
3110 |
} |
| 3111 |
|
3111 |
|
| 3112 |
/// Return true if 'V & Mask' is known to be zero. We use this predicate to |
3112 |
/// Return true if 'V & Mask' is known to be zero. We use this predicate to |
| 3113 |
/// simplify operations downstream. Mask is known to be zero for bits that V |
3113 |
/// simplify operations downstream. Mask is known to be zero for bits that V |
| 3114 |
/// cannot have. |
3114 |
/// cannot have. |
| 3115 |
/// |
3115 |
/// |
| 3116 |
/// This function is defined on values with integer type, values with pointer |
3116 |
/// This function is defined on values with integer type, values with pointer |
| 3117 |
/// type, and vectors of integers. In the case |
3117 |
/// type, and vectors of integers. In the case |
| 3118 |
/// where V is a vector, the mask, known zero, and known one values are the |
3118 |
/// where V is a vector, the mask, known zero, and known one values are the |
| 3119 |
/// same width as the vector element, and the bit is set only if it is true |
3119 |
/// same width as the vector element, and the bit is set only if it is true |
| 3120 |
/// for all of the elements in the vector. |
3120 |
/// for all of the elements in the vector. |
| 3121 |
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, |
3121 |
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, |
| 3122 |
const SimplifyQuery &Q) { |
3122 |
const SimplifyQuery &Q) { |
| 3123 |
KnownBits Known(Mask.getBitWidth()); |
3123 |
KnownBits Known(Mask.getBitWidth()); |
| 3124 |
computeKnownBits(V, Known, Depth, Q); |
3124 |
computeKnownBits(V, Known, Depth, Q); |
| 3125 |
return Mask.isSubsetOf(Known.Zero); |
3125 |
return Mask.isSubsetOf(Known.Zero); |
| 3126 |
} |
3126 |
} |
| 3127 |
|
3127 |
|
| 3128 |
// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow). |
3128 |
// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow). |
| 3129 |
// Returns the input and lower/upper bounds. |
3129 |
// Returns the input and lower/upper bounds. |
| 3130 |
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, |
3130 |
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, |
| 3131 |
const APInt *&CLow, const APInt *&CHigh) { |
3131 |
const APInt *&CLow, const APInt *&CHigh) { |
| 3132 |
assert(isa(Select) && |
3132 |
assert(isa(Select) && |
| 3133 |
cast(Select)->getOpcode() == Instruction::Select && |
3133 |
cast(Select)->getOpcode() == Instruction::Select && |
| 3134 |
"Input should be a Select!"); |
3134 |
"Input should be a Select!"); |
| 3135 |
|
3135 |
|
| 3136 |
const Value *LHS = nullptr, *RHS = nullptr; |
3136 |
const Value *LHS = nullptr, *RHS = nullptr; |
| 3137 |
SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor; |
3137 |
SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor; |
| 3138 |
if (SPF != SPF_SMAX && SPF != SPF_SMIN) |
3138 |
if (SPF != SPF_SMAX && SPF != SPF_SMIN) |
| 3139 |
return false; |
3139 |
return false; |
| 3140 |
|
3140 |
|
| 3141 |
if (!match(RHS, m_APInt(CLow))) |
3141 |
if (!match(RHS, m_APInt(CLow))) |
| 3142 |
return false; |
3142 |
return false; |
| 3143 |
|
3143 |
|
| 3144 |
const Value *LHS2 = nullptr, *RHS2 = nullptr; |
3144 |
const Value *LHS2 = nullptr, *RHS2 = nullptr; |
| 3145 |
SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor; |
3145 |
SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor; |
| 3146 |
if (getInverseMinMaxFlavor(SPF) != SPF2) |
3146 |
if (getInverseMinMaxFlavor(SPF) != SPF2) |
| 3147 |
return false; |
3147 |
return false; |
| 3148 |
|
3148 |
|
| 3149 |
if (!match(RHS2, m_APInt(CHigh))) |
3149 |
if (!match(RHS2, m_APInt(CHigh))) |
| 3150 |
return false; |
3150 |
return false; |
| 3151 |
|
3151 |
|
| 3152 |
if (SPF == SPF_SMIN) |
3152 |
if (SPF == SPF_SMIN) |
| 3153 |
std::swap(CLow, CHigh); |
3153 |
std::swap(CLow, CHigh); |
| 3154 |
|
3154 |
|
| 3155 |
In = LHS2; |
3155 |
In = LHS2; |
| 3156 |
return CLow->sle(*CHigh); |
3156 |
return CLow->sle(*CHigh); |
| 3157 |
} |
3157 |
} |
| 3158 |
|
3158 |
|
| 3159 |
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, |
3159 |
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, |
| 3160 |
const APInt *&CLow, |
3160 |
const APInt *&CLow, |
| 3161 |
const APInt *&CHigh) { |
3161 |
const APInt *&CHigh) { |
| 3162 |
assert((II->getIntrinsicID() == Intrinsic::smin || |
3162 |
assert((II->getIntrinsicID() == Intrinsic::smin || |
| 3163 |
II->getIntrinsicID() == Intrinsic::smax) && "Must be smin/smax"); |
3163 |
II->getIntrinsicID() == Intrinsic::smax) && "Must be smin/smax"); |
| 3164 |
|
3164 |
|
| 3165 |
Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID()); |
3165 |
Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID()); |
| 3166 |
auto *InnerII = dyn_cast(II->getArgOperand(0)); |
3166 |
auto *InnerII = dyn_cast(II->getArgOperand(0)); |
| 3167 |
if (!InnerII || InnerII->getIntrinsicID() != InverseID || |
3167 |
if (!InnerII || InnerII->getIntrinsicID() != InverseID || |
| 3168 |
!match(II->getArgOperand(1), m_APInt(CLow)) || |
3168 |
!match(II->getArgOperand(1), m_APInt(CLow)) || |
| 3169 |
!match(InnerII->getArgOperand(1), m_APInt(CHigh))) |
3169 |
!match(InnerII->getArgOperand(1), m_APInt(CHigh))) |
| 3170 |
return false; |
3170 |
return false; |
| 3171 |
|
3171 |
|
| 3172 |
if (II->getIntrinsicID() == Intrinsic::smin) |
3172 |
if (II->getIntrinsicID() == Intrinsic::smin) |
| 3173 |
std::swap(CLow, CHigh); |
3173 |
std::swap(CLow, CHigh); |
| 3174 |
return CLow->sle(*CHigh); |
3174 |
return CLow->sle(*CHigh); |
| 3175 |
} |
3175 |
} |
| 3176 |
|
3176 |
|
| 3177 |
/// For vector constants, loop over the elements and find the constant with the |
3177 |
/// For vector constants, loop over the elements and find the constant with the |
| 3178 |
/// minimum number of sign bits. Return 0 if the value is not a vector constant |
3178 |
/// minimum number of sign bits. Return 0 if the value is not a vector constant |
| 3179 |
/// or if any element was not analyzed; otherwise, return the count for the |
3179 |
/// or if any element was not analyzed; otherwise, return the count for the |
| 3180 |
/// element with the minimum number of sign bits. |
3180 |
/// element with the minimum number of sign bits. |
| 3181 |
static unsigned computeNumSignBitsVectorConstant(const Value *V, |
3181 |
static unsigned computeNumSignBitsVectorConstant(const Value *V, |
| 3182 |
const APInt &DemandedElts, |
3182 |
const APInt &DemandedElts, |
| 3183 |
unsigned TyBits) { |
3183 |
unsigned TyBits) { |
| 3184 |
const auto *CV = dyn_cast(V); |
3184 |
const auto *CV = dyn_cast(V); |
| 3185 |
if (!CV || !isa(CV->getType())) |
3185 |
if (!CV || !isa(CV->getType())) |
| 3186 |
return 0; |
3186 |
return 0; |
| 3187 |
|
3187 |
|
| 3188 |
unsigned MinSignBits = TyBits; |
3188 |
unsigned MinSignBits = TyBits; |
| 3189 |
unsigned NumElts = cast(CV->getType())->getNumElements(); |
3189 |
unsigned NumElts = cast(CV->getType())->getNumElements(); |
| 3190 |
for (unsigned i = 0; i != NumElts; ++i) { |
3190 |
for (unsigned i = 0; i != NumElts; ++i) { |
| 3191 |
if (!DemandedElts[i]) |
3191 |
if (!DemandedElts[i]) |
| 3192 |
continue; |
3192 |
continue; |
| 3193 |
// If we find a non-ConstantInt, bail out. |
3193 |
// If we find a non-ConstantInt, bail out. |
| 3194 |
auto *Elt = dyn_cast_or_null(CV->getAggregateElement(i)); |
3194 |
auto *Elt = dyn_cast_or_null(CV->getAggregateElement(i)); |
| 3195 |
if (!Elt) |
3195 |
if (!Elt) |
| 3196 |
return 0; |
3196 |
return 0; |
| 3197 |
|
3197 |
|
| 3198 |
MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits()); |
3198 |
MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits()); |
| 3199 |
} |
3199 |
} |
| 3200 |
|
3200 |
|
| 3201 |
return MinSignBits; |
3201 |
return MinSignBits; |
| 3202 |
} |
3202 |
} |
| 3203 |
|
3203 |
|
| 3204 |
static unsigned ComputeNumSignBitsImpl(const Value *V, |
3204 |
static unsigned ComputeNumSignBitsImpl(const Value *V, |
| 3205 |
const APInt &DemandedElts, |
3205 |
const APInt &DemandedElts, |
| 3206 |
unsigned Depth, const SimplifyQuery &Q); |
3206 |
unsigned Depth, const SimplifyQuery &Q); |
| 3207 |
|
3207 |
|
| 3208 |
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, |
3208 |
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, |
| 3209 |
unsigned Depth, const SimplifyQuery &Q) { |
3209 |
unsigned Depth, const SimplifyQuery &Q) { |
| 3210 |
unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q); |
3210 |
unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q); |
| 3211 |
assert(Result > 0 && "At least one sign bit needs to be present!"); |
3211 |
assert(Result > 0 && "At least one sign bit needs to be present!"); |
| 3212 |
return Result; |
3212 |
return Result; |
| 3213 |
} |
3213 |
} |
| 3214 |
|
3214 |
|
| 3215 |
/// Return the number of times the sign bit of the register is replicated into |
3215 |
/// Return the number of times the sign bit of the register is replicated into |
| 3216 |
/// the other bits. We know that at least 1 bit is always equal to the sign bit |
3216 |
/// the other bits. We know that at least 1 bit is always equal to the sign bit |
| 3217 |
/// (itself), but other cases can give us information. For example, immediately |
3217 |
/// (itself), but other cases can give us information. For example, immediately |
| 3218 |
/// after an "ashr X, 2", we know that the top 3 bits are all equal to each |
3218 |
/// after an "ashr X, 2", we know that the top 3 bits are all equal to each |
| 3219 |
/// other, so we return 3. For vectors, return the number of sign bits for the |
3219 |
/// other, so we return 3. For vectors, return the number of sign bits for the |
| 3220 |
/// vector element with the minimum number of known sign bits of the demanded |
3220 |
/// vector element with the minimum number of known sign bits of the demanded |
| 3221 |
/// elements in the vector specified by DemandedElts. |
3221 |
/// elements in the vector specified by DemandedElts. |
| 3222 |
static unsigned ComputeNumSignBitsImpl(const Value *V, |
3222 |
static unsigned ComputeNumSignBitsImpl(const Value *V, |
| 3223 |
const APInt &DemandedElts, |
3223 |
const APInt &DemandedElts, |
| 3224 |
unsigned Depth, const SimplifyQuery &Q) { |
3224 |
unsigned Depth, const SimplifyQuery &Q) { |
| 3225 |
Type *Ty = V->getType(); |
3225 |
Type *Ty = V->getType(); |
| 3226 |
#ifndef NDEBUG |
3226 |
#ifndef NDEBUG |
| 3227 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
3227 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
| 3228 |
|
3228 |
|
| 3229 |
if (auto *FVTy = dyn_cast(Ty)) { |
3229 |
if (auto *FVTy = dyn_cast(Ty)) { |
| 3230 |
assert( |
3230 |
assert( |
| 3231 |
FVTy->getNumElements() == DemandedElts.getBitWidth() && |
3231 |
FVTy->getNumElements() == DemandedElts.getBitWidth() && |
| 3232 |
"DemandedElt width should equal the fixed vector number of elements"); |
3232 |
"DemandedElt width should equal the fixed vector number of elements"); |
| 3233 |
} else { |
3233 |
} else { |
| 3234 |
assert(DemandedElts == APInt(1, 1) && |
3234 |
assert(DemandedElts == APInt(1, 1) && |
| 3235 |
"DemandedElt width should be 1 for scalars"); |
3235 |
"DemandedElt width should be 1 for scalars"); |
| 3236 |
} |
3236 |
} |
| 3237 |
#endif |
3237 |
#endif |
| 3238 |
|
3238 |
|
| 3239 |
// We return the minimum number of sign bits that are guaranteed to be present |
3239 |
// We return the minimum number of sign bits that are guaranteed to be present |
| 3240 |
// in V, so for undef we have to conservatively return 1. We don't have the |
3240 |
// in V, so for undef we have to conservatively return 1. We don't have the |
| 3241 |
// same behavior for poison though -- that's a FIXME today. |
3241 |
// same behavior for poison though -- that's a FIXME today. |
| 3242 |
|
3242 |
|
| 3243 |
Type *ScalarTy = Ty->getScalarType(); |
3243 |
Type *ScalarTy = Ty->getScalarType(); |
| 3244 |
unsigned TyBits = ScalarTy->isPointerTy() ? |
3244 |
unsigned TyBits = ScalarTy->isPointerTy() ? |
| 3245 |
Q.DL.getPointerTypeSizeInBits(ScalarTy) : |
3245 |
Q.DL.getPointerTypeSizeInBits(ScalarTy) : |
| 3246 |
Q.DL.getTypeSizeInBits(ScalarTy); |
3246 |
Q.DL.getTypeSizeInBits(ScalarTy); |
| 3247 |
|
3247 |
|
| 3248 |
unsigned Tmp, Tmp2; |
3248 |
unsigned Tmp, Tmp2; |
| 3249 |
unsigned FirstAnswer = 1; |
3249 |
unsigned FirstAnswer = 1; |
| 3250 |
|
3250 |
|
| 3251 |
// Note that ConstantInt is handled by the general computeKnownBits case |
3251 |
// Note that ConstantInt is handled by the general computeKnownBits case |
| 3252 |
// below. |
3252 |
// below. |
| 3253 |
|
3253 |
|
| 3254 |
if (Depth == MaxAnalysisRecursionDepth) |
3254 |
if (Depth == MaxAnalysisRecursionDepth) |
| 3255 |
return 1; |
3255 |
return 1; |
| 3256 |
|
3256 |
|
| 3257 |
if (auto *U = dyn_cast(V)) { |
3257 |
if (auto *U = dyn_cast(V)) { |
| 3258 |
switch (Operator::getOpcode(V)) { |
3258 |
switch (Operator::getOpcode(V)) { |
| 3259 |
default: break; |
3259 |
default: break; |
| 3260 |
case Instruction::SExt: |
3260 |
case Instruction::SExt: |
| 3261 |
Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); |
3261 |
Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); |
| 3262 |
return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp; |
3262 |
return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp; |
| 3263 |
|
3263 |
|
| 3264 |
case Instruction::SDiv: { |
3264 |
case Instruction::SDiv: { |
| 3265 |
const APInt *Denominator; |
3265 |
const APInt *Denominator; |
| 3266 |
// sdiv X, C -> adds log(C) sign bits. |
3266 |
// sdiv X, C -> adds log(C) sign bits. |
| 3267 |
if (match(U->getOperand(1), m_APInt(Denominator))) { |
3267 |
if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 3268 |
|
3268 |
|
| 3269 |
// Ignore non-positive denominator. |
3269 |
// Ignore non-positive denominator. |
| 3270 |
if (!Denominator->isStrictlyPositive()) |
3270 |
if (!Denominator->isStrictlyPositive()) |
| 3271 |
break; |
3271 |
break; |
| 3272 |
|
3272 |
|
| 3273 |
// Calculate the incoming numerator bits. |
3273 |
// Calculate the incoming numerator bits. |
| 3274 |
unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3274 |
unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3275 |
|
3275 |
|
| 3276 |
// Add floor(log(C)) bits to the numerator bits. |
3276 |
// Add floor(log(C)) bits to the numerator bits. |
| 3277 |
return std::min(TyBits, NumBits + Denominator->logBase2()); |
3277 |
return std::min(TyBits, NumBits + Denominator->logBase2()); |
| 3278 |
} |
3278 |
} |
| 3279 |
break; |
3279 |
break; |
| 3280 |
} |
3280 |
} |
| 3281 |
|
3281 |
|
| 3282 |
case Instruction::SRem: { |
3282 |
case Instruction::SRem: { |
| 3283 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3283 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3284 |
|
3284 |
|
| 3285 |
const APInt *Denominator; |
3285 |
const APInt *Denominator; |
| 3286 |
// srem X, C -> we know that the result is within [-C+1,C) when C is a |
3286 |
// srem X, C -> we know that the result is within [-C+1,C) when C is a |
| 3287 |
// positive constant. This let us put a lower bound on the number of sign |
3287 |
// positive constant. This let us put a lower bound on the number of sign |
| 3288 |
// bits. |
3288 |
// bits. |
| 3289 |
if (match(U->getOperand(1), m_APInt(Denominator))) { |
3289 |
if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 3290 |
|
3290 |
|
| 3291 |
// Ignore non-positive denominator. |
3291 |
// Ignore non-positive denominator. |
| 3292 |
if (Denominator->isStrictlyPositive()) { |
3292 |
if (Denominator->isStrictlyPositive()) { |
| 3293 |
// Calculate the leading sign bit constraints by examining the |
3293 |
// Calculate the leading sign bit constraints by examining the |
| 3294 |
// denominator. Given that the denominator is positive, there are two |
3294 |
// denominator. Given that the denominator is positive, there are two |
| 3295 |
// cases: |
3295 |
// cases: |
| 3296 |
// |
3296 |
// |
| 3297 |
// 1. The numerator is positive. The result range is [0,C) and |
3297 |
// 1. The numerator is positive. The result range is [0,C) and |
| 3298 |
// [0,C) u< (1 << ceilLogBase2(C)). |
3298 |
// [0,C) u< (1 << ceilLogBase2(C)). |
| 3299 |
// |
3299 |
// |
| 3300 |
// 2. The numerator is negative. Then the result range is (-C,0] and |
3300 |
// 2. The numerator is negative. Then the result range is (-C,0] and |
| 3301 |
// integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). |
3301 |
// integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). |
| 3302 |
// |
3302 |
// |
| 3303 |
// Thus a lower bound on the number of sign bits is `TyBits - |
3303 |
// Thus a lower bound on the number of sign bits is `TyBits - |
| 3304 |
// ceilLogBase2(C)`. |
3304 |
// ceilLogBase2(C)`. |
| 3305 |
|
3305 |
|
| 3306 |
unsigned ResBits = TyBits - Denominator->ceilLogBase2(); |
3306 |
unsigned ResBits = TyBits - Denominator->ceilLogBase2(); |
| 3307 |
Tmp = std::max(Tmp, ResBits); |
3307 |
Tmp = std::max(Tmp, ResBits); |
| 3308 |
} |
3308 |
} |
| 3309 |
} |
3309 |
} |
| 3310 |
return Tmp; |
3310 |
return Tmp; |
| 3311 |
} |
3311 |
} |
| 3312 |
|
3312 |
|
| 3313 |
case Instruction::AShr: { |
3313 |
case Instruction::AShr: { |
| 3314 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3314 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3315 |
// ashr X, C -> adds C sign bits. Vectors too. |
3315 |
// ashr X, C -> adds C sign bits. Vectors too. |
| 3316 |
const APInt *ShAmt; |
3316 |
const APInt *ShAmt; |
| 3317 |
if (match(U->getOperand(1), m_APInt(ShAmt))) { |
3317 |
if (match(U->getOperand(1), m_APInt(ShAmt))) { |
| 3318 |
if (ShAmt->uge(TyBits)) |
3318 |
if (ShAmt->uge(TyBits)) |
| 3319 |
break; // Bad shift. |
3319 |
break; // Bad shift. |
| 3320 |
unsigned ShAmtLimited = ShAmt->getZExtValue(); |
3320 |
unsigned ShAmtLimited = ShAmt->getZExtValue(); |
| 3321 |
Tmp += ShAmtLimited; |
3321 |
Tmp += ShAmtLimited; |
| 3322 |
if (Tmp > TyBits) Tmp = TyBits; |
3322 |
if (Tmp > TyBits) Tmp = TyBits; |
| 3323 |
} |
3323 |
} |
| 3324 |
return Tmp; |
3324 |
return Tmp; |
| 3325 |
} |
3325 |
} |
| 3326 |
case Instruction::Shl: { |
3326 |
case Instruction::Shl: { |
| 3327 |
const APInt *ShAmt; |
3327 |
const APInt *ShAmt; |
| 3328 |
if (match(U->getOperand(1), m_APInt(ShAmt))) { |
3328 |
if (match(U->getOperand(1), m_APInt(ShAmt))) { |
| 3329 |
// shl destroys sign bits. |
3329 |
// shl destroys sign bits. |
| 3330 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3330 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3331 |
if (ShAmt->uge(TyBits) || // Bad shift. |
3331 |
if (ShAmt->uge(TyBits) || // Bad shift. |
| 3332 |
ShAmt->uge(Tmp)) break; // Shifted all sign bits out. |
3332 |
ShAmt->uge(Tmp)) break; // Shifted all sign bits out. |
| 3333 |
Tmp2 = ShAmt->getZExtValue(); |
3333 |
Tmp2 = ShAmt->getZExtValue(); |
| 3334 |
return Tmp - Tmp2; |
3334 |
return Tmp - Tmp2; |
| 3335 |
} |
3335 |
} |
| 3336 |
break; |
3336 |
break; |
| 3337 |
} |
3337 |
} |
| 3338 |
case Instruction::And: |
3338 |
case Instruction::And: |
| 3339 |
case Instruction::Or: |
3339 |
case Instruction::Or: |
| 3340 |
case Instruction::Xor: // NOT is handled here. |
3340 |
case Instruction::Xor: // NOT is handled here. |
| 3341 |
// Logical binary ops preserve the number of sign bits at the worst. |
3341 |
// Logical binary ops preserve the number of sign bits at the worst. |
| 3342 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3342 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3343 |
if (Tmp != 1) { |
3343 |
if (Tmp != 1) { |
| 3344 |
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
3344 |
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
| 3345 |
FirstAnswer = std::min(Tmp, Tmp2); |
3345 |
FirstAnswer = std::min(Tmp, Tmp2); |
| 3346 |
// We computed what we know about the sign bits as our first |
3346 |
// We computed what we know about the sign bits as our first |
| 3347 |
// answer. Now proceed to the generic code that uses |
3347 |
// answer. Now proceed to the generic code that uses |
| 3348 |
// computeKnownBits, and pick whichever answer is better. |
3348 |
// computeKnownBits, and pick whichever answer is better. |
| 3349 |
} |
3349 |
} |
| 3350 |
break; |
3350 |
break; |
| 3351 |
|
3351 |
|
| 3352 |
case Instruction::Select: { |
3352 |
case Instruction::Select: { |
| 3353 |
// If we have a clamp pattern, we know that the number of sign bits will |
3353 |
// If we have a clamp pattern, we know that the number of sign bits will |
| 3354 |
// be the minimum of the clamp min/max range. |
3354 |
// be the minimum of the clamp min/max range. |
| 3355 |
const Value *X; |
3355 |
const Value *X; |
| 3356 |
const APInt *CLow, *CHigh; |
3356 |
const APInt *CLow, *CHigh; |
| 3357 |
if (isSignedMinMaxClamp(U, X, CLow, CHigh)) |
3357 |
if (isSignedMinMaxClamp(U, X, CLow, CHigh)) |
| 3358 |
return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits()); |
3358 |
return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits()); |
| 3359 |
|
3359 |
|
| 3360 |
Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
3360 |
Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
| 3361 |
if (Tmp == 1) break; |
3361 |
if (Tmp == 1) break; |
| 3362 |
Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q); |
3362 |
Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q); |
| 3363 |
return std::min(Tmp, Tmp2); |
3363 |
return std::min(Tmp, Tmp2); |
| 3364 |
} |
3364 |
} |
| 3365 |
|
3365 |
|
| 3366 |
case Instruction::Add: |
3366 |
case Instruction::Add: |
| 3367 |
// Add can have at most one carry bit. Thus we know that the output |
3367 |
// Add can have at most one carry bit. Thus we know that the output |
| 3368 |
// is, at worst, one more bit than the inputs. |
3368 |
// is, at worst, one more bit than the inputs. |
| 3369 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3369 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3370 |
if (Tmp == 1) break; |
3370 |
if (Tmp == 1) break; |
| 3371 |
|
3371 |
|
| 3372 |
// Special case decrementing a value (ADD X, -1): |
3372 |
// Special case decrementing a value (ADD X, -1): |
| 3373 |
if (const auto *CRHS = dyn_cast(U->getOperand(1))) |
3373 |
if (const auto *CRHS = dyn_cast(U->getOperand(1))) |
| 3374 |
if (CRHS->isAllOnesValue()) { |
3374 |
if (CRHS->isAllOnesValue()) { |
| 3375 |
KnownBits Known(TyBits); |
3375 |
KnownBits Known(TyBits); |
| 3376 |
computeKnownBits(U->getOperand(0), Known, Depth + 1, Q); |
3376 |
computeKnownBits(U->getOperand(0), Known, Depth + 1, Q); |
| 3377 |
|
3377 |
|
| 3378 |
// If the input is known to be 0 or 1, the output is 0/-1, which is |
3378 |
// If the input is known to be 0 or 1, the output is 0/-1, which is |
| 3379 |
// all sign bits set. |
3379 |
// all sign bits set. |
| 3380 |
if ((Known.Zero | 1).isAllOnes()) |
3380 |
if ((Known.Zero | 1).isAllOnes()) |
| 3381 |
return TyBits; |
3381 |
return TyBits; |
| 3382 |
|
3382 |
|
| 3383 |
// If we are subtracting one from a positive number, there is no carry |
3383 |
// If we are subtracting one from a positive number, there is no carry |
| 3384 |
// out of the result. |
3384 |
// out of the result. |
| 3385 |
if (Known.isNonNegative()) |
3385 |
if (Known.isNonNegative()) |
| 3386 |
return Tmp; |
3386 |
return Tmp; |
| 3387 |
} |
3387 |
} |
| 3388 |
|
3388 |
|
| 3389 |
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
3389 |
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
| 3390 |
if (Tmp2 == 1) break; |
3390 |
if (Tmp2 == 1) break; |
| 3391 |
return std::min(Tmp, Tmp2) - 1; |
3391 |
return std::min(Tmp, Tmp2) - 1; |
| 3392 |
|
3392 |
|
| 3393 |
case Instruction::Sub: |
3393 |
case Instruction::Sub: |
| 3394 |
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
3394 |
Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
| 3395 |
if (Tmp2 == 1) break; |
3395 |
if (Tmp2 == 1) break; |
| 3396 |
|
3396 |
|
| 3397 |
// Handle NEG. |
3397 |
// Handle NEG. |
| 3398 |
if (const auto *CLHS = dyn_cast(U->getOperand(0))) |
3398 |
if (const auto *CLHS = dyn_cast(U->getOperand(0))) |
| 3399 |
if (CLHS->isNullValue()) { |
3399 |
if (CLHS->isNullValue()) { |
| 3400 |
KnownBits Known(TyBits); |
3400 |
KnownBits Known(TyBits); |
| 3401 |
computeKnownBits(U->getOperand(1), Known, Depth + 1, Q); |
3401 |
computeKnownBits(U->getOperand(1), Known, Depth + 1, Q); |
| 3402 |
// If the input is known to be 0 or 1, the output is 0/-1, which is |
3402 |
// If the input is known to be 0 or 1, the output is 0/-1, which is |
| 3403 |
// all sign bits set. |
3403 |
// all sign bits set. |
| 3404 |
if ((Known.Zero | 1).isAllOnes()) |
3404 |
if ((Known.Zero | 1).isAllOnes()) |
| 3405 |
return TyBits; |
3405 |
return TyBits; |
| 3406 |
|
3406 |
|
| 3407 |
// If the input is known to be positive (the sign bit is known clear), |
3407 |
// If the input is known to be positive (the sign bit is known clear), |
| 3408 |
// the output of the NEG has the same number of sign bits as the |
3408 |
// the output of the NEG has the same number of sign bits as the |
| 3409 |
// input. |
3409 |
// input. |
| 3410 |
if (Known.isNonNegative()) |
3410 |
if (Known.isNonNegative()) |
| 3411 |
return Tmp2; |
3411 |
return Tmp2; |
| 3412 |
|
3412 |
|
| 3413 |
// Otherwise, we treat this like a SUB. |
3413 |
// Otherwise, we treat this like a SUB. |
| 3414 |
} |
3414 |
} |
| 3415 |
|
3415 |
|
| 3416 |
// Sub can have at most one carry bit. Thus we know that the output |
3416 |
// Sub can have at most one carry bit. Thus we know that the output |
| 3417 |
// is, at worst, one more bit than the inputs. |
3417 |
// is, at worst, one more bit than the inputs. |
| 3418 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3418 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3419 |
if (Tmp == 1) break; |
3419 |
if (Tmp == 1) break; |
| 3420 |
return std::min(Tmp, Tmp2) - 1; |
3420 |
return std::min(Tmp, Tmp2) - 1; |
| 3421 |
|
3421 |
|
| 3422 |
case Instruction::Mul: { |
3422 |
case Instruction::Mul: { |
| 3423 |
// The output of the Mul can be at most twice the valid bits in the |
3423 |
// The output of the Mul can be at most twice the valid bits in the |
| 3424 |
// inputs. |
3424 |
// inputs. |
| 3425 |
unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3425 |
unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3426 |
if (SignBitsOp0 == 1) break; |
3426 |
if (SignBitsOp0 == 1) break; |
| 3427 |
unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
3427 |
unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
| 3428 |
if (SignBitsOp1 == 1) break; |
3428 |
if (SignBitsOp1 == 1) break; |
| 3429 |
unsigned OutValidBits = |
3429 |
unsigned OutValidBits = |
| 3430 |
(TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1); |
3430 |
(TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1); |
| 3431 |
return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1; |
3431 |
return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1; |
| 3432 |
} |
3432 |
} |
| 3433 |
|
3433 |
|
| 3434 |
case Instruction::PHI: { |
3434 |
case Instruction::PHI: { |
| 3435 |
const PHINode *PN = cast(U); |
3435 |
const PHINode *PN = cast(U); |
| 3436 |
unsigned NumIncomingValues = PN->getNumIncomingValues(); |
3436 |
unsigned NumIncomingValues = PN->getNumIncomingValues(); |
| 3437 |
// Don't analyze large in-degree PHIs. |
3437 |
// Don't analyze large in-degree PHIs. |
| 3438 |
if (NumIncomingValues > 4) break; |
3438 |
if (NumIncomingValues > 4) break; |
| 3439 |
// Unreachable blocks may have zero-operand PHI nodes. |
3439 |
// Unreachable blocks may have zero-operand PHI nodes. |
| 3440 |
if (NumIncomingValues == 0) break; |
3440 |
if (NumIncomingValues == 0) break; |
| 3441 |
|
3441 |
|
| 3442 |
// Take the minimum of all incoming values. This can't infinitely loop |
3442 |
// Take the minimum of all incoming values. This can't infinitely loop |
| 3443 |
// because of our depth threshold. |
3443 |
// because of our depth threshold. |
| 3444 |
SimplifyQuery RecQ = Q; |
3444 |
SimplifyQuery RecQ = Q; |
| 3445 |
Tmp = TyBits; |
3445 |
Tmp = TyBits; |
| 3446 |
for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) { |
3446 |
for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) { |
| 3447 |
if (Tmp == 1) return Tmp; |
3447 |
if (Tmp == 1) return Tmp; |
| 3448 |
RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator(); |
3448 |
RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator(); |
| 3449 |
Tmp = std::min( |
3449 |
Tmp = std::min( |
| 3450 |
Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, RecQ)); |
3450 |
Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, RecQ)); |
| 3451 |
} |
3451 |
} |
| 3452 |
return Tmp; |
3452 |
return Tmp; |
| 3453 |
} |
3453 |
} |
| 3454 |
|
3454 |
|
| 3455 |
case Instruction::Trunc: { |
3455 |
case Instruction::Trunc: { |
| 3456 |
// If the input contained enough sign bits that some remain after the |
3456 |
// If the input contained enough sign bits that some remain after the |
| 3457 |
// truncation, then we can make use of that. Otherwise we don't know |
3457 |
// truncation, then we can make use of that. Otherwise we don't know |
| 3458 |
// anything. |
3458 |
// anything. |
| 3459 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3459 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3460 |
unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits(); |
3460 |
unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits(); |
| 3461 |
if (Tmp > (OperandTyBits - TyBits)) |
3461 |
if (Tmp > (OperandTyBits - TyBits)) |
| 3462 |
return Tmp - (OperandTyBits - TyBits); |
3462 |
return Tmp - (OperandTyBits - TyBits); |
| 3463 |
|
3463 |
|
| 3464 |
return 1; |
3464 |
return 1; |
| 3465 |
} |
3465 |
} |
| 3466 |
|
3466 |
|
| 3467 |
case Instruction::ExtractElement: |
3467 |
case Instruction::ExtractElement: |
| 3468 |
// Look through extract element. At the moment we keep this simple and |
3468 |
// Look through extract element. At the moment we keep this simple and |
| 3469 |
// skip tracking the specific element. But at least we might find |
3469 |
// skip tracking the specific element. But at least we might find |
| 3470 |
// information valid for all elements of the vector (for example if vector |
3470 |
// information valid for all elements of the vector (for example if vector |
| 3471 |
// is sign extended, shifted, etc). |
3471 |
// is sign extended, shifted, etc). |
| 3472 |
return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3472 |
return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3473 |
|
3473 |
|
| 3474 |
case Instruction::ShuffleVector: { |
3474 |
case Instruction::ShuffleVector: { |
| 3475 |
// Collect the minimum number of sign bits that are shared by every vector |
3475 |
// Collect the minimum number of sign bits that are shared by every vector |
| 3476 |
// element referenced by the shuffle. |
3476 |
// element referenced by the shuffle. |
| 3477 |
auto *Shuf = dyn_cast(U); |
3477 |
auto *Shuf = dyn_cast(U); |
| 3478 |
if (!Shuf) { |
3478 |
if (!Shuf) { |
| 3479 |
// FIXME: Add support for shufflevector constant expressions. |
3479 |
// FIXME: Add support for shufflevector constant expressions. |
| 3480 |
return 1; |
3480 |
return 1; |
| 3481 |
} |
3481 |
} |
| 3482 |
APInt DemandedLHS, DemandedRHS; |
3482 |
APInt DemandedLHS, DemandedRHS; |
| 3483 |
// For undef elements, we don't know anything about the common state of |
3483 |
// For undef elements, we don't know anything about the common state of |
| 3484 |
// the shuffle result. |
3484 |
// the shuffle result. |
| 3485 |
if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) |
3485 |
if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) |
| 3486 |
return 1; |
3486 |
return 1; |
| 3487 |
Tmp = std::numeric_limits::max(); |
3487 |
Tmp = std::numeric_limits::max(); |
| 3488 |
if (!!DemandedLHS) { |
3488 |
if (!!DemandedLHS) { |
| 3489 |
const Value *LHS = Shuf->getOperand(0); |
3489 |
const Value *LHS = Shuf->getOperand(0); |
| 3490 |
Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q); |
3490 |
Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q); |
| 3491 |
} |
3491 |
} |
| 3492 |
// If we don't know anything, early out and try computeKnownBits |
3492 |
// If we don't know anything, early out and try computeKnownBits |
| 3493 |
// fall-back. |
3493 |
// fall-back. |
| 3494 |
if (Tmp == 1) |
3494 |
if (Tmp == 1) |
| 3495 |
break; |
3495 |
break; |
| 3496 |
if (!!DemandedRHS) { |
3496 |
if (!!DemandedRHS) { |
| 3497 |
const Value *RHS = Shuf->getOperand(1); |
3497 |
const Value *RHS = Shuf->getOperand(1); |
| 3498 |
Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q); |
3498 |
Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q); |
| 3499 |
Tmp = std::min(Tmp, Tmp2); |
3499 |
Tmp = std::min(Tmp, Tmp2); |
| 3500 |
} |
3500 |
} |
| 3501 |
// If we don't know anything, early out and try computeKnownBits |
3501 |
// If we don't know anything, early out and try computeKnownBits |
| 3502 |
// fall-back. |
3502 |
// fall-back. |
| 3503 |
if (Tmp == 1) |
3503 |
if (Tmp == 1) |
| 3504 |
break; |
3504 |
break; |
| 3505 |
assert(Tmp <= TyBits && "Failed to determine minimum sign bits"); |
3505 |
assert(Tmp <= TyBits && "Failed to determine minimum sign bits"); |
| 3506 |
return Tmp; |
3506 |
return Tmp; |
| 3507 |
} |
3507 |
} |
| 3508 |
case Instruction::Call: { |
3508 |
case Instruction::Call: { |
| 3509 |
if (const auto *II = dyn_cast(U)) { |
3509 |
if (const auto *II = dyn_cast(U)) { |
| 3510 |
switch (II->getIntrinsicID()) { |
3510 |
switch (II->getIntrinsicID()) { |
| 3511 |
default: break; |
3511 |
default: break; |
| 3512 |
case Intrinsic::abs: |
3512 |
case Intrinsic::abs: |
| 3513 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
3513 |
Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
| 3514 |
if (Tmp == 1) break; |
3514 |
if (Tmp == 1) break; |
| 3515 |
|
3515 |
|
| 3516 |
// Absolute value reduces number of sign bits by at most 1. |
3516 |
// Absolute value reduces number of sign bits by at most 1. |
| 3517 |
return Tmp - 1; |
3517 |
return Tmp - 1; |
| 3518 |
case Intrinsic::smin: |
3518 |
case Intrinsic::smin: |
| 3519 |
case Intrinsic::smax: { |
3519 |
case Intrinsic::smax: { |
| 3520 |
const APInt *CLow, *CHigh; |
3520 |
const APInt *CLow, *CHigh; |
| 3521 |
if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh)) |
3521 |
if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh)) |
| 3522 |
return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits()); |
3522 |
return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits()); |
| 3523 |
} |
3523 |
} |
| 3524 |
} |
3524 |
} |
| 3525 |
} |
3525 |
} |
| 3526 |
} |
3526 |
} |
| 3527 |
} |
3527 |
} |
| 3528 |
} |
3528 |
} |
| 3529 |
|
3529 |
|
| 3530 |
// Finally, if we can prove that the top bits of the result are 0's or 1's, |
3530 |
// Finally, if we can prove that the top bits of the result are 0's or 1's, |
| 3531 |
// use this information. |
3531 |
// use this information. |
| 3532 |
|
3532 |
|
| 3533 |
// If we can examine all elements of a vector constant successfully, we're |
3533 |
// If we can examine all elements of a vector constant successfully, we're |
| 3534 |
// done (we can't do any better than that). If not, keep trying. |
3534 |
// done (we can't do any better than that). If not, keep trying. |
| 3535 |
if (unsigned VecSignBits = |
3535 |
if (unsigned VecSignBits = |
| 3536 |
computeNumSignBitsVectorConstant(V, DemandedElts, TyBits)) |
3536 |
computeNumSignBitsVectorConstant(V, DemandedElts, TyBits)) |
| 3537 |
return VecSignBits; |
3537 |
return VecSignBits; |
| 3538 |
|
3538 |
|
| 3539 |
KnownBits Known(TyBits); |
3539 |
KnownBits Known(TyBits); |
| 3540 |
computeKnownBits(V, DemandedElts, Known, Depth, Q); |
3540 |
computeKnownBits(V, DemandedElts, Known, Depth, Q); |
| 3541 |
|
3541 |
|
| 3542 |
// If we know that the sign bit is either zero or one, determine the number of |
3542 |
// If we know that the sign bit is either zero or one, determine the number of |
| 3543 |
// identical bits in the top of the input value. |
3543 |
// identical bits in the top of the input value. |
| 3544 |
return std::max(FirstAnswer, Known.countMinSignBits()); |
3544 |
return std::max(FirstAnswer, Known.countMinSignBits()); |
| 3545 |
} |
3545 |
} |
| 3546 |
|
3546 |
|
| 3547 |
Intrinsic::ID llvm::getIntrinsicForCallSite(const CallBase &CB, |
3547 |
Intrinsic::ID llvm::getIntrinsicForCallSite(const CallBase &CB, |
| 3548 |
const TargetLibraryInfo *TLI) { |
3548 |
const TargetLibraryInfo *TLI) { |
| 3549 |
const Function *F = CB.getCalledFunction(); |
3549 |
const Function *F = CB.getCalledFunction(); |
| 3550 |
if (!F) |
3550 |
if (!F) |
| 3551 |
return Intrinsic::not_intrinsic; |
3551 |
return Intrinsic::not_intrinsic; |
| 3552 |
|
3552 |
|
| 3553 |
if (F->isIntrinsic()) |
3553 |
if (F->isIntrinsic()) |
| 3554 |
return F->getIntrinsicID(); |
3554 |
return F->getIntrinsicID(); |
| 3555 |
|
3555 |
|
| 3556 |
// We are going to infer semantics of a library function based on mapping it |
3556 |
// We are going to infer semantics of a library function based on mapping it |
| 3557 |
// to an LLVM intrinsic. Check that the library function is available from |
3557 |
// to an LLVM intrinsic. Check that the library function is available from |
| 3558 |
// this callbase and in this environment. |
3558 |
// this callbase and in this environment. |
| 3559 |
LibFunc Func; |
3559 |
LibFunc Func; |
| 3560 |
if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) || |
3560 |
if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) || |
| 3561 |
!CB.onlyReadsMemory()) |
3561 |
!CB.onlyReadsMemory()) |
| 3562 |
return Intrinsic::not_intrinsic; |
3562 |
return Intrinsic::not_intrinsic; |
| 3563 |
|
3563 |
|
| 3564 |
switch (Func) { |
3564 |
switch (Func) { |
| 3565 |
default: |
3565 |
default: |
| 3566 |
break; |
3566 |
break; |
| 3567 |
case LibFunc_sin: |
3567 |
case LibFunc_sin: |
| 3568 |
case LibFunc_sinf: |
3568 |
case LibFunc_sinf: |
| 3569 |
case LibFunc_sinl: |
3569 |
case LibFunc_sinl: |
| 3570 |
return Intrinsic::sin; |
3570 |
return Intrinsic::sin; |
| 3571 |
case LibFunc_cos: |
3571 |
case LibFunc_cos: |
| 3572 |
case LibFunc_cosf: |
3572 |
case LibFunc_cosf: |
| 3573 |
case LibFunc_cosl: |
3573 |
case LibFunc_cosl: |
| 3574 |
return Intrinsic::cos; |
3574 |
return Intrinsic::cos; |
| 3575 |
case LibFunc_exp: |
3575 |
case LibFunc_exp: |
| 3576 |
case LibFunc_expf: |
3576 |
case LibFunc_expf: |
| 3577 |
case LibFunc_expl: |
3577 |
case LibFunc_expl: |
| 3578 |
return Intrinsic::exp; |
3578 |
return Intrinsic::exp; |
| 3579 |
case LibFunc_exp2: |
3579 |
case LibFunc_exp2: |
| 3580 |
case LibFunc_exp2f: |
3580 |
case LibFunc_exp2f: |
| 3581 |
case LibFunc_exp2l: |
3581 |
case LibFunc_exp2l: |
| 3582 |
return Intrinsic::exp2; |
3582 |
return Intrinsic::exp2; |
| 3583 |
case LibFunc_log: |
3583 |
case LibFunc_log: |
| 3584 |
case LibFunc_logf: |
3584 |
case LibFunc_logf: |
| 3585 |
case LibFunc_logl: |
3585 |
case LibFunc_logl: |
| 3586 |
return Intrinsic::log; |
3586 |
return Intrinsic::log; |
| 3587 |
case LibFunc_log10: |
3587 |
case LibFunc_log10: |
| 3588 |
case LibFunc_log10f: |
3588 |
case LibFunc_log10f: |
| 3589 |
case LibFunc_log10l: |
3589 |
case LibFunc_log10l: |
| 3590 |
return Intrinsic::log10; |
3590 |
return Intrinsic::log10; |
| 3591 |
case LibFunc_log2: |
3591 |
case LibFunc_log2: |
| 3592 |
case LibFunc_log2f: |
3592 |
case LibFunc_log2f: |
| 3593 |
case LibFunc_log2l: |
3593 |
case LibFunc_log2l: |
| 3594 |
return Intrinsic::log2; |
3594 |
return Intrinsic::log2; |
| 3595 |
case LibFunc_fabs: |
3595 |
case LibFunc_fabs: |
| 3596 |
case LibFunc_fabsf: |
3596 |
case LibFunc_fabsf: |
| 3597 |
case LibFunc_fabsl: |
3597 |
case LibFunc_fabsl: |
| 3598 |
return Intrinsic::fabs; |
3598 |
return Intrinsic::fabs; |
| 3599 |
case LibFunc_fmin: |
3599 |
case LibFunc_fmin: |
| 3600 |
case LibFunc_fminf: |
3600 |
case LibFunc_fminf: |
| 3601 |
case LibFunc_fminl: |
3601 |
case LibFunc_fminl: |
| 3602 |
return Intrinsic::minnum; |
3602 |
return Intrinsic::minnum; |
| 3603 |
case LibFunc_fmax: |
3603 |
case LibFunc_fmax: |
| 3604 |
case LibFunc_fmaxf: |
3604 |
case LibFunc_fmaxf: |
| 3605 |
case LibFunc_fmaxl: |
3605 |
case LibFunc_fmaxl: |
| 3606 |
return Intrinsic::maxnum; |
3606 |
return Intrinsic::maxnum; |
| 3607 |
case LibFunc_copysign: |
3607 |
case LibFunc_copysign: |
| 3608 |
case LibFunc_copysignf: |
3608 |
case LibFunc_copysignf: |
| 3609 |
case LibFunc_copysignl: |
3609 |
case LibFunc_copysignl: |
| 3610 |
return Intrinsic::copysign; |
3610 |
return Intrinsic::copysign; |
| 3611 |
case LibFunc_floor: |
3611 |
case LibFunc_floor: |
| 3612 |
case LibFunc_floorf: |
3612 |
case LibFunc_floorf: |
| 3613 |
case LibFunc_floorl: |
3613 |
case LibFunc_floorl: |
| 3614 |
return Intrinsic::floor; |
3614 |
return Intrinsic::floor; |
| 3615 |
case LibFunc_ceil: |
3615 |
case LibFunc_ceil: |
| 3616 |
case LibFunc_ceilf: |
3616 |
case LibFunc_ceilf: |
| 3617 |
case LibFunc_ceill: |
3617 |
case LibFunc_ceill: |
| 3618 |
return Intrinsic::ceil; |
3618 |
return Intrinsic::ceil; |
| 3619 |
case LibFunc_trunc: |
3619 |
case LibFunc_trunc: |
| 3620 |
case LibFunc_truncf: |
3620 |
case LibFunc_truncf: |
| 3621 |
case LibFunc_truncl: |
3621 |
case LibFunc_truncl: |
| 3622 |
return Intrinsic::trunc; |
3622 |
return Intrinsic::trunc; |
| 3623 |
case LibFunc_rint: |
3623 |
case LibFunc_rint: |
| 3624 |
case LibFunc_rintf: |
3624 |
case LibFunc_rintf: |
| 3625 |
case LibFunc_rintl: |
3625 |
case LibFunc_rintl: |
| 3626 |
return Intrinsic::rint; |
3626 |
return Intrinsic::rint; |
| 3627 |
case LibFunc_nearbyint: |
3627 |
case LibFunc_nearbyint: |
| 3628 |
case LibFunc_nearbyintf: |
3628 |
case LibFunc_nearbyintf: |
| 3629 |
case LibFunc_nearbyintl: |
3629 |
case LibFunc_nearbyintl: |
| 3630 |
return Intrinsic::nearbyint; |
3630 |
return Intrinsic::nearbyint; |
| 3631 |
case LibFunc_round: |
3631 |
case LibFunc_round: |
| 3632 |
case LibFunc_roundf: |
3632 |
case LibFunc_roundf: |
| 3633 |
case LibFunc_roundl: |
3633 |
case LibFunc_roundl: |
| 3634 |
return Intrinsic::round; |
3634 |
return Intrinsic::round; |
| 3635 |
case LibFunc_roundeven: |
3635 |
case LibFunc_roundeven: |
| 3636 |
case LibFunc_roundevenf: |
3636 |
case LibFunc_roundevenf: |
| 3637 |
case LibFunc_roundevenl: |
3637 |
case LibFunc_roundevenl: |
| 3638 |
return Intrinsic::roundeven; |
3638 |
return Intrinsic::roundeven; |
| 3639 |
case LibFunc_pow: |
3639 |
case LibFunc_pow: |
| 3640 |
case LibFunc_powf: |
3640 |
case LibFunc_powf: |
| 3641 |
case LibFunc_powl: |
3641 |
case LibFunc_powl: |
| 3642 |
return Intrinsic::pow; |
3642 |
return Intrinsic::pow; |
| 3643 |
case LibFunc_sqrt: |
3643 |
case LibFunc_sqrt: |
| 3644 |
case LibFunc_sqrtf: |
3644 |
case LibFunc_sqrtf: |
| 3645 |
case LibFunc_sqrtl: |
3645 |
case LibFunc_sqrtl: |
| 3646 |
return Intrinsic::sqrt; |
3646 |
return Intrinsic::sqrt; |
| 3647 |
} |
3647 |
} |
| 3648 |
|
3648 |
|
| 3649 |
return Intrinsic::not_intrinsic; |
3649 |
return Intrinsic::not_intrinsic; |
| 3650 |
} |
3650 |
} |
| 3651 |
|
3651 |
|
| 3652 |
/// If \p SignBitOnly is true, test for a known 0 sign bit rather than a |
3652 |
/// If \p SignBitOnly is true, test for a known 0 sign bit rather than a |
| 3653 |
/// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign |
3653 |
/// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign |
| 3654 |
/// bit despite comparing equal. |
3654 |
/// bit despite comparing equal. |
| 3655 |
static bool cannotBeOrderedLessThanZeroImpl(const Value *V, |
3655 |
static bool cannotBeOrderedLessThanZeroImpl(const Value *V, |
| 3656 |
const DataLayout &DL, |
3656 |
const DataLayout &DL, |
| 3657 |
const TargetLibraryInfo *TLI, |
3657 |
const TargetLibraryInfo *TLI, |
| 3658 |
bool SignBitOnly, unsigned Depth) { |
3658 |
bool SignBitOnly, unsigned Depth) { |
| 3659 |
// TODO: This function does not do the right thing when SignBitOnly is true |
3659 |
// TODO: This function does not do the right thing when SignBitOnly is true |
| 3660 |
// and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform |
3660 |
// and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform |
| 3661 |
// which flips the sign bits of NaNs. See |
3661 |
// which flips the sign bits of NaNs. See |
| 3662 |
// https://llvm.org/bugs/show_bug.cgi?id=31702. |
3662 |
// https://llvm.org/bugs/show_bug.cgi?id=31702. |
| 3663 |
|
3663 |
|
| 3664 |
if (const ConstantFP *CFP = dyn_cast(V)) { |
3664 |
if (const ConstantFP *CFP = dyn_cast(V)) { |
| 3665 |
return !CFP->getValueAPF().isNegative() || |
3665 |
return !CFP->getValueAPF().isNegative() || |
| 3666 |
(!SignBitOnly && CFP->getValueAPF().isZero()); |
3666 |
(!SignBitOnly && CFP->getValueAPF().isZero()); |
| 3667 |
} |
3667 |
} |
| 3668 |
|
3668 |
|
| 3669 |
// Handle vector of constants. |
3669 |
// Handle vector of constants. |
| 3670 |
if (auto *CV = dyn_cast(V)) { |
3670 |
if (auto *CV = dyn_cast(V)) { |
| 3671 |
if (auto *CVFVTy = dyn_cast(CV->getType())) { |
3671 |
if (auto *CVFVTy = dyn_cast(CV->getType())) { |
| 3672 |
unsigned NumElts = CVFVTy->getNumElements(); |
3672 |
unsigned NumElts = CVFVTy->getNumElements(); |
| 3673 |
for (unsigned i = 0; i != NumElts; ++i) { |
3673 |
for (unsigned i = 0; i != NumElts; ++i) { |
| 3674 |
auto *CFP = dyn_cast_or_null(CV->getAggregateElement(i)); |
3674 |
auto *CFP = dyn_cast_or_null(CV->getAggregateElement(i)); |
| 3675 |
if (!CFP) |
3675 |
if (!CFP) |
| 3676 |
return false; |
3676 |
return false; |
| 3677 |
if (CFP->getValueAPF().isNegative() && |
3677 |
if (CFP->getValueAPF().isNegative() && |
| 3678 |
(SignBitOnly || !CFP->getValueAPF().isZero())) |
3678 |
(SignBitOnly || !CFP->getValueAPF().isZero())) |
| 3679 |
return false; |
3679 |
return false; |
| 3680 |
} |
3680 |
} |
| 3681 |
|
3681 |
|
| 3682 |
// All non-negative ConstantFPs. |
3682 |
// All non-negative ConstantFPs. |
| 3683 |
return true; |
3683 |
return true; |
| 3684 |
} |
3684 |
} |
| 3685 |
} |
3685 |
} |
| 3686 |
|
3686 |
|
| 3687 |
if (Depth == MaxAnalysisRecursionDepth) |
3687 |
if (Depth == MaxAnalysisRecursionDepth) |
| 3688 |
return false; |
3688 |
return false; |
| 3689 |
|
3689 |
|
| 3690 |
const Operator *I = dyn_cast(V); |
3690 |
const Operator *I = dyn_cast(V); |
| 3691 |
if (!I) |
3691 |
if (!I) |
| 3692 |
return false; |
3692 |
return false; |
| 3693 |
|
3693 |
|
| 3694 |
switch (I->getOpcode()) { |
3694 |
switch (I->getOpcode()) { |
| 3695 |
default: |
3695 |
default: |
| 3696 |
break; |
3696 |
break; |
| 3697 |
// Unsigned integers are always nonnegative. |
3697 |
// Unsigned integers are always nonnegative. |
| 3698 |
case Instruction::UIToFP: |
3698 |
case Instruction::UIToFP: |
| 3699 |
return true; |
3699 |
return true; |
| 3700 |
case Instruction::FDiv: |
3700 |
case Instruction::FDiv: |
| 3701 |
// X / X is always exactly 1.0 or a NaN. |
3701 |
// X / X is always exactly 1.0 or a NaN. |
| 3702 |
if (I->getOperand(0) == I->getOperand(1) && |
3702 |
if (I->getOperand(0) == I->getOperand(1) && |
| 3703 |
(!SignBitOnly || cast(I)->hasNoNaNs())) |
3703 |
(!SignBitOnly || cast(I)->hasNoNaNs())) |
| 3704 |
return true; |
3704 |
return true; |
| 3705 |
|
3705 |
|
| 3706 |
// Set SignBitOnly for RHS, because X / -0.0 is -Inf (or NaN). |
3706 |
// Set SignBitOnly for RHS, because X / -0.0 is -Inf (or NaN). |
| 3707 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
3707 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
| 3708 |
SignBitOnly, Depth + 1) && |
3708 |
SignBitOnly, Depth + 1) && |
| 3709 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
3709 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
| 3710 |
/*SignBitOnly*/ true, Depth + 1); |
3710 |
/*SignBitOnly*/ true, Depth + 1); |
| 3711 |
case Instruction::FMul: |
3711 |
case Instruction::FMul: |
| 3712 |
// X * X is always non-negative or a NaN. |
3712 |
// X * X is always non-negative or a NaN. |
| 3713 |
if (I->getOperand(0) == I->getOperand(1) && |
3713 |
if (I->getOperand(0) == I->getOperand(1) && |
| 3714 |
(!SignBitOnly || cast(I)->hasNoNaNs())) |
3714 |
(!SignBitOnly || cast(I)->hasNoNaNs())) |
| 3715 |
return true; |
3715 |
return true; |
| 3716 |
|
3716 |
|
| 3717 |
[[fallthrough]]; |
3717 |
[[fallthrough]]; |
| 3718 |
case Instruction::FAdd: |
3718 |
case Instruction::FAdd: |
| 3719 |
case Instruction::FRem: |
3719 |
case Instruction::FRem: |
| 3720 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
3720 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
| 3721 |
SignBitOnly, Depth + 1) && |
3721 |
SignBitOnly, Depth + 1) && |
| 3722 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
3722 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
| 3723 |
SignBitOnly, Depth + 1); |
3723 |
SignBitOnly, Depth + 1); |
| 3724 |
case Instruction::Select: |
3724 |
case Instruction::Select: |
| 3725 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
3725 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
| 3726 |
SignBitOnly, Depth + 1) && |
3726 |
SignBitOnly, Depth + 1) && |
| 3727 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(2), DL, TLI, |
3727 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(2), DL, TLI, |
| 3728 |
SignBitOnly, Depth + 1); |
3728 |
SignBitOnly, Depth + 1); |
| 3729 |
case Instruction::FPExt: |
3729 |
case Instruction::FPExt: |
| 3730 |
case Instruction::FPTrunc: |
3730 |
case Instruction::FPTrunc: |
| 3731 |
// Widening/narrowing never change sign. |
3731 |
// Widening/narrowing never change sign. |
| 3732 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
3732 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
| 3733 |
SignBitOnly, Depth + 1); |
3733 |
SignBitOnly, Depth + 1); |
| 3734 |
case Instruction::ExtractElement: |
3734 |
case Instruction::ExtractElement: |
| 3735 |
// Look through extract element. At the moment we keep this simple and skip |
3735 |
// Look through extract element. At the moment we keep this simple and skip |
| 3736 |
// tracking the specific element. But at least we might find information |
3736 |
// tracking the specific element. But at least we might find information |
| 3737 |
// valid for all elements of the vector. |
3737 |
// valid for all elements of the vector. |
| 3738 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
3738 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
| 3739 |
SignBitOnly, Depth + 1); |
3739 |
SignBitOnly, Depth + 1); |
| 3740 |
case Instruction::Call: |
3740 |
case Instruction::Call: |
| 3741 |
const auto *CI = cast(I); |
3741 |
const auto *CI = cast(I); |
| 3742 |
Intrinsic::ID IID = getIntrinsicForCallSite(*CI, TLI); |
3742 |
Intrinsic::ID IID = getIntrinsicForCallSite(*CI, TLI); |
| 3743 |
switch (IID) { |
3743 |
switch (IID) { |
| 3744 |
default: |
3744 |
default: |
| 3745 |
break; |
3745 |
break; |
| 3746 |
case Intrinsic::canonicalize: |
3746 |
case Intrinsic::canonicalize: |
| 3747 |
case Intrinsic::arithmetic_fence: |
3747 |
case Intrinsic::arithmetic_fence: |
| 3748 |
case Intrinsic::floor: |
3748 |
case Intrinsic::floor: |
| 3749 |
case Intrinsic::ceil: |
3749 |
case Intrinsic::ceil: |
| 3750 |
case Intrinsic::trunc: |
3750 |
case Intrinsic::trunc: |
| 3751 |
case Intrinsic::rint: |
3751 |
case Intrinsic::rint: |
| 3752 |
case Intrinsic::nearbyint: |
3752 |
case Intrinsic::nearbyint: |
| 3753 |
case Intrinsic::round: |
3753 |
case Intrinsic::round: |
| 3754 |
case Intrinsic::roundeven: |
3754 |
case Intrinsic::roundeven: |
| 3755 |
case Intrinsic::fptrunc_round: |
3755 |
case Intrinsic::fptrunc_round: |
| 3756 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
3756 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
| 3757 |
SignBitOnly, Depth + 1); |
3757 |
SignBitOnly, Depth + 1); |
| 3758 |
case Intrinsic::maxnum: { |
3758 |
case Intrinsic::maxnum: { |
| 3759 |
Value *V0 = I->getOperand(0), *V1 = I->getOperand(1); |
3759 |
Value *V0 = I->getOperand(0), *V1 = I->getOperand(1); |
| 3760 |
auto isPositiveNum = [&](Value *V) { |
3760 |
auto isPositiveNum = [&](Value *V) { |
| 3761 |
if (SignBitOnly) { |
3761 |
if (SignBitOnly) { |
| 3762 |
// With SignBitOnly, this is tricky because the result of |
3762 |
// With SignBitOnly, this is tricky because the result of |
| 3763 |
// maxnum(+0.0, -0.0) is unspecified. Just check if the operand is |
3763 |
// maxnum(+0.0, -0.0) is unspecified. Just check if the operand is |
| 3764 |
// a constant strictly greater than 0.0. |
3764 |
// a constant strictly greater than 0.0. |
| 3765 |
const APFloat *C; |
3765 |
const APFloat *C; |
| 3766 |
return match(V, m_APFloat(C)) && |
3766 |
return match(V, m_APFloat(C)) && |
| 3767 |
*C > APFloat::getZero(C->getSemantics()); |
3767 |
*C > APFloat::getZero(C->getSemantics()); |
| 3768 |
} |
3768 |
} |
| 3769 |
|
3769 |
|
| 3770 |
// -0.0 compares equal to 0.0, so if this operand is at least -0.0, |
3770 |
// -0.0 compares equal to 0.0, so if this operand is at least -0.0, |
| 3771 |
// maxnum can't be ordered-less-than-zero. |
3771 |
// maxnum can't be ordered-less-than-zero. |
| 3772 |
return isKnownNeverNaN(V, DL, TLI) && |
3772 |
return isKnownNeverNaN(V, DL, TLI) && |
| 3773 |
cannotBeOrderedLessThanZeroImpl(V, DL, TLI, false, Depth + 1); |
3773 |
cannotBeOrderedLessThanZeroImpl(V, DL, TLI, false, Depth + 1); |
| 3774 |
}; |
3774 |
}; |
| 3775 |
|
3775 |
|
| 3776 |
// TODO: This could be improved. We could also check that neither operand |
3776 |
// TODO: This could be improved. We could also check that neither operand |
| 3777 |
// has its sign bit set (and at least 1 is not-NAN?). |
3777 |
// has its sign bit set (and at least 1 is not-NAN?). |
| 3778 |
return isPositiveNum(V0) || isPositiveNum(V1); |
3778 |
return isPositiveNum(V0) || isPositiveNum(V1); |
| 3779 |
} |
3779 |
} |
| 3780 |
|
3780 |
|
| 3781 |
case Intrinsic::maximum: |
3781 |
case Intrinsic::maximum: |
| 3782 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
3782 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
| 3783 |
SignBitOnly, Depth + 1) || |
3783 |
SignBitOnly, Depth + 1) || |
| 3784 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
3784 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
| 3785 |
SignBitOnly, Depth + 1); |
3785 |
SignBitOnly, Depth + 1); |
| 3786 |
case Intrinsic::minnum: |
3786 |
case Intrinsic::minnum: |
| 3787 |
case Intrinsic::minimum: |
3787 |
case Intrinsic::minimum: |
| 3788 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
3788 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
| 3789 |
SignBitOnly, Depth + 1) && |
3789 |
SignBitOnly, Depth + 1) && |
| 3790 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
3790 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, |
| 3791 |
SignBitOnly, Depth + 1); |
3791 |
SignBitOnly, Depth + 1); |
| 3792 |
case Intrinsic::exp: |
3792 |
case Intrinsic::exp: |
| 3793 |
case Intrinsic::exp2: |
3793 |
case Intrinsic::exp2: |
| 3794 |
case Intrinsic::fabs: |
3794 |
case Intrinsic::fabs: |
| 3795 |
return true; |
3795 |
return true; |
| 3796 |
case Intrinsic::copysign: |
3796 |
case Intrinsic::copysign: |
| 3797 |
// Only the sign operand matters. |
3797 |
// Only the sign operand matters. |
| 3798 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, true, |
3798 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), DL, TLI, true, |
| 3799 |
Depth + 1); |
3799 |
Depth + 1); |
| 3800 |
case Intrinsic::sqrt: |
3800 |
case Intrinsic::sqrt: |
| 3801 |
// sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0. |
3801 |
// sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0. |
| 3802 |
if (!SignBitOnly) |
3802 |
if (!SignBitOnly) |
| 3803 |
return true; |
3803 |
return true; |
| 3804 |
return CI->hasNoNaNs() && |
3804 |
return CI->hasNoNaNs() && |
| 3805 |
(CI->hasNoSignedZeros() || |
3805 |
(CI->hasNoSignedZeros() || |
| 3806 |
cannotBeNegativeZero(CI->getOperand(0), DL, TLI)); |
3806 |
cannotBeNegativeZero(CI->getOperand(0), DL, TLI)); |
| 3807 |
|
3807 |
|
| 3808 |
case Intrinsic::powi: |
3808 |
case Intrinsic::powi: |
| 3809 |
if (ConstantInt *Exponent = dyn_cast(I->getOperand(1))) { |
3809 |
if (ConstantInt *Exponent = dyn_cast(I->getOperand(1))) { |
| 3810 |
// powi(x,n) is non-negative if n is even. |
3810 |
// powi(x,n) is non-negative if n is even. |
| 3811 |
if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0) |
3811 |
if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0) |
| 3812 |
return true; |
3812 |
return true; |
| 3813 |
} |
3813 |
} |
| 3814 |
// TODO: This is not correct. Given that exp is an integer, here are the |
3814 |
// TODO: This is not correct. Given that exp is an integer, here are the |
| 3815 |
// ways that pow can return a negative value: |
3815 |
// ways that pow can return a negative value: |
| 3816 |
// |
3816 |
// |
| 3817 |
// pow(x, exp) --> negative if exp is odd and x is negative. |
3817 |
// pow(x, exp) --> negative if exp is odd and x is negative. |
| 3818 |
// pow(-0, exp) --> -inf if exp is negative odd. |
3818 |
// pow(-0, exp) --> -inf if exp is negative odd. |
| 3819 |
// pow(-0, exp) --> -0 if exp is positive odd. |
3819 |
// pow(-0, exp) --> -0 if exp is positive odd. |
| 3820 |
// pow(-inf, exp) --> -0 if exp is negative odd. |
3820 |
// pow(-inf, exp) --> -0 if exp is negative odd. |
| 3821 |
// pow(-inf, exp) --> -inf if exp is positive odd. |
3821 |
// pow(-inf, exp) --> -inf if exp is positive odd. |
| 3822 |
// |
3822 |
// |
| 3823 |
// Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN, |
3823 |
// Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN, |
| 3824 |
// but we must return false if x == -0. Unfortunately we do not currently |
3824 |
// but we must return false if x == -0. Unfortunately we do not currently |
| 3825 |
// have a way of expressing this constraint. See details in |
3825 |
// have a way of expressing this constraint. See details in |
| 3826 |
// https://llvm.org/bugs/show_bug.cgi?id=31702. |
3826 |
// https://llvm.org/bugs/show_bug.cgi?id=31702. |
| 3827 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
3827 |
return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), DL, TLI, |
| 3828 |
SignBitOnly, Depth + 1); |
3828 |
SignBitOnly, Depth + 1); |
| 3829 |
|
3829 |
|
| 3830 |
case Intrinsic::fma: |
3830 |
case Intrinsic::fma: |
| 3831 |
case Intrinsic::fmuladd: |
3831 |
case Intrinsic::fmuladd: |
| 3832 |
// x*x+y is non-negative if y is non-negative. |
3832 |
// x*x+y is non-negative if y is non-negative. |
| 3833 |
return I->getOperand(0) == I->getOperand(1) && |
3833 |
return I->getOperand(0) == I->getOperand(1) && |
| 3834 |
(!SignBitOnly || cast(I)->hasNoNaNs()) && |
3834 |
(!SignBitOnly || cast(I)->hasNoNaNs()) && |
| 3835 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(2), DL, TLI, |
3835 |
cannotBeOrderedLessThanZeroImpl(I->getOperand(2), DL, TLI, |
| 3836 |
SignBitOnly, Depth + 1); |
3836 |
SignBitOnly, Depth + 1); |
| 3837 |
} |
3837 |
} |
| 3838 |
break; |
3838 |
break; |
| 3839 |
} |
3839 |
} |
| 3840 |
return false; |
3840 |
return false; |
| 3841 |
} |
3841 |
} |
| 3842 |
|
3842 |
|
| 3843 |
bool llvm::CannotBeOrderedLessThanZero(const Value *V, const DataLayout &DL, |
3843 |
bool llvm::CannotBeOrderedLessThanZero(const Value *V, const DataLayout &DL, |
| 3844 |
const TargetLibraryInfo *TLI) { |
3844 |
const TargetLibraryInfo *TLI) { |
| 3845 |
return cannotBeOrderedLessThanZeroImpl(V, DL, TLI, false, 0); |
3845 |
return cannotBeOrderedLessThanZeroImpl(V, DL, TLI, false, 0); |
| 3846 |
} |
3846 |
} |
| 3847 |
|
3847 |
|
| 3848 |
bool llvm::SignBitMustBeZero(const Value *V, const DataLayout &DL, |
3848 |
bool llvm::SignBitMustBeZero(const Value *V, const DataLayout &DL, |
| 3849 |
const TargetLibraryInfo *TLI) { |
3849 |
const TargetLibraryInfo *TLI) { |
| 3850 |
return cannotBeOrderedLessThanZeroImpl(V, DL, TLI, true, 0); |
3850 |
return cannotBeOrderedLessThanZeroImpl(V, DL, TLI, true, 0); |
| 3851 |
} |
3851 |
} |
| 3852 |
|
3852 |
|
| 3853 |
/// Return true if it's possible to assume IEEE treatment of input denormals in |
3853 |
/// Return true if it's possible to assume IEEE treatment of input denormals in |
| 3854 |
/// \p F for \p Val. |
3854 |
/// \p F for \p Val. |
| 3855 |
static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) { |
3855 |
static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) { |
| 3856 |
Ty = Ty->getScalarType(); |
3856 |
Ty = Ty->getScalarType(); |
| 3857 |
return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE; |
3857 |
return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE; |
| 3858 |
} |
3858 |
} |
| 3859 |
|
3859 |
|
| 3860 |
static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) { |
3860 |
static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) { |
| 3861 |
Ty = Ty->getScalarType(); |
3861 |
Ty = Ty->getScalarType(); |
| 3862 |
DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics()); |
3862 |
DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics()); |
| 3863 |
return Mode.Input == DenormalMode::IEEE || |
3863 |
return Mode.Input == DenormalMode::IEEE || |
| 3864 |
Mode.Input == DenormalMode::PositiveZero; |
3864 |
Mode.Input == DenormalMode::PositiveZero; |
| 3865 |
} |
3865 |
} |
| 3866 |
|
3866 |
|
| 3867 |
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) { |
3867 |
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) { |
| 3868 |
Ty = Ty->getScalarType(); |
3868 |
Ty = Ty->getScalarType(); |
| 3869 |
DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics()); |
3869 |
DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics()); |
| 3870 |
return Mode.Output == DenormalMode::IEEE || |
3870 |
return Mode.Output == DenormalMode::IEEE || |
| 3871 |
Mode.Output == DenormalMode::PositiveZero; |
3871 |
Mode.Output == DenormalMode::PositiveZero; |
| 3872 |
} |
3872 |
} |
| 3873 |
|
3873 |
|
| 3874 |
bool KnownFPClass::isKnownNeverLogicalZero(const Function &F, Type *Ty) const { |
3874 |
bool KnownFPClass::isKnownNeverLogicalZero(const Function &F, Type *Ty) const { |
| 3875 |
return isKnownNeverZero() && |
3875 |
return isKnownNeverZero() && |
| 3876 |
(isKnownNeverSubnormal() || inputDenormalIsIEEE(F, Ty)); |
3876 |
(isKnownNeverSubnormal() || inputDenormalIsIEEE(F, Ty)); |
| 3877 |
} |
3877 |
} |
| 3878 |
|
3878 |
|
| 3879 |
bool KnownFPClass::isKnownNeverLogicalNegZero(const Function &F, |
3879 |
bool KnownFPClass::isKnownNeverLogicalNegZero(const Function &F, |
| 3880 |
Type *Ty) const { |
3880 |
Type *Ty) const { |
| 3881 |
return isKnownNeverNegZero() && |
3881 |
return isKnownNeverNegZero() && |
| 3882 |
(isKnownNeverNegSubnormal() || inputDenormalIsIEEEOrPosZero(F, Ty)); |
3882 |
(isKnownNeverNegSubnormal() || inputDenormalIsIEEEOrPosZero(F, Ty)); |
| 3883 |
} |
3883 |
} |
| 3884 |
|
3884 |
|
| 3885 |
bool KnownFPClass::isKnownNeverLogicalPosZero(const Function &F, |
3885 |
bool KnownFPClass::isKnownNeverLogicalPosZero(const Function &F, |
| 3886 |
Type *Ty) const { |
3886 |
Type *Ty) const { |
| 3887 |
if (!isKnownNeverPosZero()) |
3887 |
if (!isKnownNeverPosZero()) |
| 3888 |
return false; |
3888 |
return false; |
| 3889 |
|
3889 |
|
| 3890 |
// If we know there are no denormals, nothing can be flushed to zero. |
3890 |
// If we know there are no denormals, nothing can be flushed to zero. |
| 3891 |
if (isKnownNeverSubnormal()) |
3891 |
if (isKnownNeverSubnormal()) |
| 3892 |
return true; |
3892 |
return true; |
| 3893 |
|
3893 |
|
| 3894 |
DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics()); |
3894 |
DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics()); |
| 3895 |
switch (Mode.Input) { |
3895 |
switch (Mode.Input) { |
| 3896 |
case DenormalMode::IEEE: |
3896 |
case DenormalMode::IEEE: |
| 3897 |
return true; |
3897 |
return true; |
| 3898 |
case DenormalMode::PreserveSign: |
3898 |
case DenormalMode::PreserveSign: |
| 3899 |
// Negative subnormal won't flush to +0 |
3899 |
// Negative subnormal won't flush to +0 |
| 3900 |
return isKnownNeverPosSubnormal(); |
3900 |
return isKnownNeverPosSubnormal(); |
| 3901 |
case DenormalMode::PositiveZero: |
3901 |
case DenormalMode::PositiveZero: |
| 3902 |
default: |
3902 |
default: |
| 3903 |
// Both positive and negative subnormal could flush to +0 |
3903 |
// Both positive and negative subnormal could flush to +0 |
| 3904 |
return false; |
3904 |
return false; |
| 3905 |
} |
3905 |
} |
| 3906 |
|
3906 |
|
| 3907 |
llvm_unreachable("covered switch over denormal mode"); |
3907 |
llvm_unreachable("covered switch over denormal mode"); |
| 3908 |
} |
3908 |
} |
| 3909 |
|
3909 |
|
| 3910 |
void KnownFPClass::propagateDenormal(const KnownFPClass &Src, const Function &F, |
3910 |
void KnownFPClass::propagateDenormal(const KnownFPClass &Src, const Function &F, |
| 3911 |
Type *Ty) { |
3911 |
Type *Ty) { |
| 3912 |
KnownFPClasses = Src.KnownFPClasses; |
3912 |
KnownFPClasses = Src.KnownFPClasses; |
| 3913 |
// If we aren't assuming the source can't be a zero, we don't have to check if |
3913 |
// If we aren't assuming the source can't be a zero, we don't have to check if |
| 3914 |
// a denormal input could be flushed. |
3914 |
// a denormal input could be flushed. |
| 3915 |
if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero()) |
3915 |
if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero()) |
| 3916 |
return; |
3916 |
return; |
| 3917 |
|
3917 |
|
| 3918 |
// If we know the input can't be a denormal, it can't be flushed to 0. |
3918 |
// If we know the input can't be a denormal, it can't be flushed to 0. |
| 3919 |
if (Src.isKnownNeverSubnormal()) |
3919 |
if (Src.isKnownNeverSubnormal()) |
| 3920 |
return; |
3920 |
return; |
| 3921 |
|
3921 |
|
| 3922 |
DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics()); |
3922 |
DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics()); |
| 3923 |
|
3923 |
|
| 3924 |
if (!Src.isKnownNeverPosSubnormal() && Mode != DenormalMode::getIEEE()) |
3924 |
if (!Src.isKnownNeverPosSubnormal() && Mode != DenormalMode::getIEEE()) |
| 3925 |
KnownFPClasses |= fcPosZero; |
3925 |
KnownFPClasses |= fcPosZero; |
| 3926 |
|
3926 |
|
| 3927 |
if (!Src.isKnownNeverNegSubnormal() && Mode != DenormalMode::getIEEE()) { |
3927 |
if (!Src.isKnownNeverNegSubnormal() && Mode != DenormalMode::getIEEE()) { |
| 3928 |
if (Mode != DenormalMode::getPositiveZero()) |
3928 |
if (Mode != DenormalMode::getPositiveZero()) |
| 3929 |
KnownFPClasses |= fcNegZero; |
3929 |
KnownFPClasses |= fcNegZero; |
| 3930 |
|
3930 |
|
| 3931 |
if (Mode.Input == DenormalMode::PositiveZero || |
3931 |
if (Mode.Input == DenormalMode::PositiveZero || |
| 3932 |
Mode.Output == DenormalMode::PositiveZero || |
3932 |
Mode.Output == DenormalMode::PositiveZero || |
| 3933 |
Mode.Input == DenormalMode::Dynamic || |
3933 |
Mode.Input == DenormalMode::Dynamic || |
| 3934 |
Mode.Output == DenormalMode::Dynamic) |
3934 |
Mode.Output == DenormalMode::Dynamic) |
| 3935 |
KnownFPClasses |= fcPosZero; |
3935 |
KnownFPClasses |= fcPosZero; |
| 3936 |
} |
3936 |
} |
| 3937 |
} |
3937 |
} |
| 3938 |
|
3938 |
|
| 3939 |
void KnownFPClass::propagateCanonicalizingSrc(const KnownFPClass &Src, |
3939 |
void KnownFPClass::propagateCanonicalizingSrc(const KnownFPClass &Src, |
| 3940 |
const Function &F, Type *Ty) { |
3940 |
const Function &F, Type *Ty) { |
| 3941 |
propagateDenormal(Src, F, Ty); |
3941 |
propagateDenormal(Src, F, Ty); |
| 3942 |
propagateNaN(Src, /*PreserveSign=*/true); |
3942 |
propagateNaN(Src, /*PreserveSign=*/true); |
| 3943 |
} |
3943 |
} |
| 3944 |
|
3944 |
|
| 3945 |
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the |
3945 |
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the |
| 3946 |
/// same result as an fcmp with the given operands. |
3946 |
/// same result as an fcmp with the given operands. |
| 3947 |
std::pair llvm::fcmpToClassTest(FCmpInst::Predicate Pred, |
3947 |
std::pair llvm::fcmpToClassTest(FCmpInst::Predicate Pred, |
| 3948 |
const Function &F, |
3948 |
const Function &F, |
| 3949 |
Value *LHS, Value *RHS, |
3949 |
Value *LHS, Value *RHS, |
| 3950 |
bool LookThroughSrc) { |
3950 |
bool LookThroughSrc) { |
| 3951 |
const APFloat *ConstRHS; |
3951 |
const APFloat *ConstRHS; |
| 3952 |
if (!match(RHS, m_APFloat(ConstRHS))) |
3952 |
if (!match(RHS, m_APFloat(ConstRHS))) |
| 3953 |
return {nullptr, fcNone}; |
3953 |
return {nullptr, fcNone}; |
| 3954 |
|
3954 |
|
| 3955 |
// fcmp ord x, zero|normal|subnormal|inf -> ~fcNan |
3955 |
// fcmp ord x, zero|normal|subnormal|inf -> ~fcNan |
| 3956 |
if (Pred == FCmpInst::FCMP_ORD && !ConstRHS->isNaN()) |
3956 |
if (Pred == FCmpInst::FCMP_ORD && !ConstRHS->isNaN()) |
| 3957 |
return {LHS, ~fcNan}; |
3957 |
return {LHS, ~fcNan}; |
| 3958 |
|
3958 |
|
| 3959 |
// fcmp uno x, zero|normal|subnormal|inf -> fcNan |
3959 |
// fcmp uno x, zero|normal|subnormal|inf -> fcNan |
| 3960 |
if (Pred == FCmpInst::FCMP_UNO && !ConstRHS->isNaN()) |
3960 |
if (Pred == FCmpInst::FCMP_UNO && !ConstRHS->isNaN()) |
| 3961 |
return {LHS, fcNan}; |
3961 |
return {LHS, fcNan}; |
| 3962 |
|
3962 |
|
| 3963 |
if (ConstRHS->isZero()) { |
3963 |
if (ConstRHS->isZero()) { |
| 3964 |
// Compares with fcNone are only exactly equal to fcZero if input denormals |
3964 |
// Compares with fcNone are only exactly equal to fcZero if input denormals |
| 3965 |
// are not flushed. |
3965 |
// are not flushed. |
| 3966 |
// TODO: Handle DAZ by expanding masks to cover subnormal cases. |
3966 |
// TODO: Handle DAZ by expanding masks to cover subnormal cases. |
| 3967 |
if (Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO && |
3967 |
if (Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO && |
| 3968 |
!inputDenormalIsIEEE(F, LHS->getType())) |
3968 |
!inputDenormalIsIEEE(F, LHS->getType())) |
| 3969 |
return {nullptr, fcNone}; |
3969 |
return {nullptr, fcNone}; |
| 3970 |
|
3970 |
|
| 3971 |
switch (Pred) { |
3971 |
switch (Pred) { |
| 3972 |
case FCmpInst::FCMP_OEQ: // Match x == 0.0 |
3972 |
case FCmpInst::FCMP_OEQ: // Match x == 0.0 |
| 3973 |
return {LHS, fcZero}; |
3973 |
return {LHS, fcZero}; |
| 3974 |
case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0) |
3974 |
case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0) |
| 3975 |
return {LHS, fcZero | fcNan}; |
3975 |
return {LHS, fcZero | fcNan}; |
| 3976 |
case FCmpInst::FCMP_UNE: // Match (x != 0.0) |
3976 |
case FCmpInst::FCMP_UNE: // Match (x != 0.0) |
| 3977 |
return {LHS, ~fcZero}; |
3977 |
return {LHS, ~fcZero}; |
| 3978 |
case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0 |
3978 |
case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0 |
| 3979 |
return {LHS, ~fcNan & ~fcZero}; |
3979 |
return {LHS, ~fcNan & ~fcZero}; |
| 3980 |
case FCmpInst::FCMP_ORD: |
3980 |
case FCmpInst::FCMP_ORD: |
| 3981 |
// Canonical form of ord/uno is with a zero. We could also handle |
3981 |
// Canonical form of ord/uno is with a zero. We could also handle |
| 3982 |
// non-canonical other non-NaN constants or LHS == RHS. |
3982 |
// non-canonical other non-NaN constants or LHS == RHS. |
| 3983 |
return {LHS, ~fcNan}; |
3983 |
return {LHS, ~fcNan}; |
| 3984 |
case FCmpInst::FCMP_UNO: |
3984 |
case FCmpInst::FCMP_UNO: |
| 3985 |
return {LHS, fcNan}; |
3985 |
return {LHS, fcNan}; |
| 3986 |
case FCmpInst::FCMP_OGT: // x > 0 |
3986 |
case FCmpInst::FCMP_OGT: // x > 0 |
| 3987 |
return {LHS, fcPosSubnormal | fcPosNormal | fcPosInf}; |
3987 |
return {LHS, fcPosSubnormal | fcPosNormal | fcPosInf}; |
| 3988 |
case FCmpInst::FCMP_UGT: // isnan(x) || x > 0 |
3988 |
case FCmpInst::FCMP_UGT: // isnan(x) || x > 0 |
| 3989 |
return {LHS, fcPosSubnormal | fcPosNormal | fcPosInf | fcNan}; |
3989 |
return {LHS, fcPosSubnormal | fcPosNormal | fcPosInf | fcNan}; |
| 3990 |
case FCmpInst::FCMP_OGE: // x >= 0 |
3990 |
case FCmpInst::FCMP_OGE: // x >= 0 |
| 3991 |
return {LHS, fcPositive | fcNegZero}; |
3991 |
return {LHS, fcPositive | fcNegZero}; |
| 3992 |
case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0 |
3992 |
case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0 |
| 3993 |
return {LHS, fcPositive | fcNegZero | fcNan}; |
3993 |
return {LHS, fcPositive | fcNegZero | fcNan}; |
| 3994 |
case FCmpInst::FCMP_OLT: // x < 0 |
3994 |
case FCmpInst::FCMP_OLT: // x < 0 |
| 3995 |
return {LHS, fcNegSubnormal | fcNegNormal | fcNegInf}; |
3995 |
return {LHS, fcNegSubnormal | fcNegNormal | fcNegInf}; |
| 3996 |
case FCmpInst::FCMP_ULT: // isnan(x) || x < 0 |
3996 |
case FCmpInst::FCMP_ULT: // isnan(x) || x < 0 |
| 3997 |
return {LHS, fcNegSubnormal | fcNegNormal | fcNegInf | fcNan}; |
3997 |
return {LHS, fcNegSubnormal | fcNegNormal | fcNegInf | fcNan}; |
| 3998 |
case FCmpInst::FCMP_OLE: // x <= 0 |
3998 |
case FCmpInst::FCMP_OLE: // x <= 0 |
| 3999 |
return {LHS, fcNegative | fcPosZero}; |
3999 |
return {LHS, fcNegative | fcPosZero}; |
| 4000 |
case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0 |
4000 |
case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0 |
| 4001 |
return {LHS, fcNegative | fcPosZero | fcNan}; |
4001 |
return {LHS, fcNegative | fcPosZero | fcNan}; |
| 4002 |
default: |
4002 |
default: |
| 4003 |
break; |
4003 |
break; |
| 4004 |
} |
4004 |
} |
| 4005 |
|
4005 |
|
| 4006 |
return {nullptr, fcNone}; |
4006 |
return {nullptr, fcNone}; |
| 4007 |
} |
4007 |
} |
| 4008 |
|
4008 |
|
| 4009 |
Value *Src = LHS; |
4009 |
Value *Src = LHS; |
| 4010 |
const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src))); |
4010 |
const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src))); |
| 4011 |
|
4011 |
|
| 4012 |
// Compute the test mask that would return true for the ordered comparisons. |
4012 |
// Compute the test mask that would return true for the ordered comparisons. |
| 4013 |
FPClassTest Mask; |
4013 |
FPClassTest Mask; |
| 4014 |
|
4014 |
|
| 4015 |
if (ConstRHS->isInfinity()) { |
4015 |
if (ConstRHS->isInfinity()) { |
| 4016 |
switch (Pred) { |
4016 |
switch (Pred) { |
| 4017 |
case FCmpInst::FCMP_OEQ: |
4017 |
case FCmpInst::FCMP_OEQ: |
| 4018 |
case FCmpInst::FCMP_UNE: { |
4018 |
case FCmpInst::FCMP_UNE: { |
| 4019 |
// Match __builtin_isinf patterns |
4019 |
// Match __builtin_isinf patterns |
| 4020 |
// |
4020 |
// |
| 4021 |
// fcmp oeq x, +inf -> is_fpclass x, fcPosInf |
4021 |
// fcmp oeq x, +inf -> is_fpclass x, fcPosInf |
| 4022 |
// fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf |
4022 |
// fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf |
| 4023 |
// fcmp oeq x, -inf -> is_fpclass x, fcNegInf |
4023 |
// fcmp oeq x, -inf -> is_fpclass x, fcNegInf |
| 4024 |
// fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false |
4024 |
// fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false |
| 4025 |
// |
4025 |
// |
| 4026 |
// fcmp une x, +inf -> is_fpclass x, ~fcPosInf |
4026 |
// fcmp une x, +inf -> is_fpclass x, ~fcPosInf |
| 4027 |
// fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf |
4027 |
// fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf |
| 4028 |
// fcmp une x, -inf -> is_fpclass x, ~fcNegInf |
4028 |
// fcmp une x, -inf -> is_fpclass x, ~fcNegInf |
| 4029 |
// fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true |
4029 |
// fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true |
| 4030 |
|
4030 |
|
| 4031 |
if (ConstRHS->isNegative()) { |
4031 |
if (ConstRHS->isNegative()) { |
| 4032 |
Mask = fcNegInf; |
4032 |
Mask = fcNegInf; |
| 4033 |
if (IsFabs) |
4033 |
if (IsFabs) |
| 4034 |
Mask = fcNone; |
4034 |
Mask = fcNone; |
| 4035 |
} else { |
4035 |
} else { |
| 4036 |
Mask = fcPosInf; |
4036 |
Mask = fcPosInf; |
| 4037 |
if (IsFabs) |
4037 |
if (IsFabs) |
| 4038 |
Mask |= fcNegInf; |
4038 |
Mask |= fcNegInf; |
| 4039 |
} |
4039 |
} |
| 4040 |
|
4040 |
|
| 4041 |
break; |
4041 |
break; |
| 4042 |
} |
4042 |
} |
| 4043 |
case FCmpInst::FCMP_ONE: |
4043 |
case FCmpInst::FCMP_ONE: |
| 4044 |
case FCmpInst::FCMP_UEQ: { |
4044 |
case FCmpInst::FCMP_UEQ: { |
| 4045 |
// Match __builtin_isinf patterns |
4045 |
// Match __builtin_isinf patterns |
| 4046 |
// fcmp one x, -inf -> is_fpclass x, fcNegInf |
4046 |
// fcmp one x, -inf -> is_fpclass x, fcNegInf |
| 4047 |
// fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan |
4047 |
// fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan |
| 4048 |
// fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan |
4048 |
// fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan |
| 4049 |
// fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan |
4049 |
// fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan |
| 4050 |
// |
4050 |
// |
| 4051 |
// fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan |
4051 |
// fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan |
| 4052 |
// fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan |
4052 |
// fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan |
| 4053 |
// fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan |
4053 |
// fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan |
| 4054 |
// fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan |
4054 |
// fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan |
| 4055 |
if (ConstRHS->isNegative()) { |
4055 |
if (ConstRHS->isNegative()) { |
| 4056 |
Mask = ~fcNegInf & ~fcNan; |
4056 |
Mask = ~fcNegInf & ~fcNan; |
| 4057 |
if (IsFabs) |
4057 |
if (IsFabs) |
| 4058 |
Mask = ~fcNan; |
4058 |
Mask = ~fcNan; |
| 4059 |
} else { |
4059 |
} else { |
| 4060 |
Mask = ~fcPosInf & ~fcNan; |
4060 |
Mask = ~fcPosInf & ~fcNan; |
| 4061 |
if (IsFabs) |
4061 |
if (IsFabs) |
| 4062 |
Mask &= ~fcNegInf; |
4062 |
Mask &= ~fcNegInf; |
| 4063 |
} |
4063 |
} |
| 4064 |
|
4064 |
|
| 4065 |
break; |
4065 |
break; |
| 4066 |
} |
4066 |
} |
| 4067 |
case FCmpInst::FCMP_OLT: |
4067 |
case FCmpInst::FCMP_OLT: |
| 4068 |
case FCmpInst::FCMP_UGE: { |
4068 |
case FCmpInst::FCMP_UGE: { |
| 4069 |
if (ConstRHS->isNegative()) { |
4069 |
if (ConstRHS->isNegative()) { |
| 4070 |
// No value is ordered and less than negative infinity. |
4070 |
// No value is ordered and less than negative infinity. |
| 4071 |
// All values are unordered with or at least negative infinity. |
4071 |
// All values are unordered with or at least negative infinity. |
| 4072 |
// fcmp olt x, -inf -> false |
4072 |
// fcmp olt x, -inf -> false |
| 4073 |
// fcmp uge x, -inf -> true |
4073 |
// fcmp uge x, -inf -> true |
| 4074 |
Mask = fcNone; |
4074 |
Mask = fcNone; |
| 4075 |
break; |
4075 |
break; |
| 4076 |
} |
4076 |
} |
| 4077 |
|
4077 |
|
| 4078 |
// fcmp olt fabs(x), +inf -> fcFinite |
4078 |
// fcmp olt fabs(x), +inf -> fcFinite |
| 4079 |
// fcmp uge fabs(x), +inf -> ~fcFinite |
4079 |
// fcmp uge fabs(x), +inf -> ~fcFinite |
| 4080 |
// fcmp olt x, +inf -> fcFinite|fcNegInf |
4080 |
// fcmp olt x, +inf -> fcFinite|fcNegInf |
| 4081 |
// fcmp uge x, +inf -> ~(fcFinite|fcNegInf) |
4081 |
// fcmp uge x, +inf -> ~(fcFinite|fcNegInf) |
| 4082 |
Mask = fcFinite; |
4082 |
Mask = fcFinite; |
| 4083 |
if (!IsFabs) |
4083 |
if (!IsFabs) |
| 4084 |
Mask |= fcNegInf; |
4084 |
Mask |= fcNegInf; |
| 4085 |
break; |
4085 |
break; |
| 4086 |
} |
4086 |
} |
| 4087 |
case FCmpInst::FCMP_OGE: |
4087 |
case FCmpInst::FCMP_OGE: |
| 4088 |
case FCmpInst::FCMP_ULT: { |
4088 |
case FCmpInst::FCMP_ULT: { |
| 4089 |
if (ConstRHS->isNegative()) // TODO |
4089 |
if (ConstRHS->isNegative()) // TODO |
| 4090 |
return {nullptr, fcNone}; |
4090 |
return {nullptr, fcNone}; |
| 4091 |
|
4091 |
|
| 4092 |
// fcmp oge fabs(x), +inf -> fcInf |
4092 |
// fcmp oge fabs(x), +inf -> fcInf |
| 4093 |
// fcmp oge x, +inf -> fcPosInf |
4093 |
// fcmp oge x, +inf -> fcPosInf |
| 4094 |
// fcmp ult fabs(x), +inf -> ~fcInf |
4094 |
// fcmp ult fabs(x), +inf -> ~fcInf |
| 4095 |
// fcmp ult x, +inf -> ~fcPosInf |
4095 |
// fcmp ult x, +inf -> ~fcPosInf |
| 4096 |
Mask = fcPosInf; |
4096 |
Mask = fcPosInf; |
| 4097 |
if (IsFabs) |
4097 |
if (IsFabs) |
| 4098 |
Mask |= fcNegInf; |
4098 |
Mask |= fcNegInf; |
| 4099 |
break; |
4099 |
break; |
| 4100 |
} |
4100 |
} |
| 4101 |
case FCmpInst::FCMP_OGT: |
4101 |
case FCmpInst::FCMP_OGT: |
| 4102 |
case FCmpInst::FCMP_ULE: { |
4102 |
case FCmpInst::FCMP_ULE: { |
| 4103 |
if (ConstRHS->isNegative()) |
4103 |
if (ConstRHS->isNegative()) |
| 4104 |
return {nullptr, fcNone}; |
4104 |
return {nullptr, fcNone}; |
| 4105 |
|
4105 |
|
| 4106 |
// No value is ordered and greater than infinity. |
4106 |
// No value is ordered and greater than infinity. |
| 4107 |
Mask = fcNone; |
4107 |
Mask = fcNone; |
| 4108 |
break; |
4108 |
break; |
| 4109 |
} |
4109 |
} |
| 4110 |
default: |
4110 |
default: |
| 4111 |
return {nullptr, fcNone}; |
4111 |
return {nullptr, fcNone}; |
| 4112 |
} |
4112 |
} |
| 4113 |
} else if (ConstRHS->isSmallestNormalized() && !ConstRHS->isNegative()) { |
4113 |
} else if (ConstRHS->isSmallestNormalized() && !ConstRHS->isNegative()) { |
| 4114 |
// Match pattern that's used in __builtin_isnormal. |
4114 |
// Match pattern that's used in __builtin_isnormal. |
| 4115 |
switch (Pred) { |
4115 |
switch (Pred) { |
| 4116 |
case FCmpInst::FCMP_OLT: |
4116 |
case FCmpInst::FCMP_OLT: |
| 4117 |
case FCmpInst::FCMP_UGE: { |
4117 |
case FCmpInst::FCMP_UGE: { |
| 4118 |
// fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero |
4118 |
// fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero |
| 4119 |
// fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero |
4119 |
// fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero |
| 4120 |
// fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf |
4120 |
// fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf |
| 4121 |
// fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero) |
4121 |
// fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero) |
| 4122 |
Mask = fcZero | fcSubnormal; |
4122 |
Mask = fcZero | fcSubnormal; |
| 4123 |
if (!IsFabs) |
4123 |
if (!IsFabs) |
| 4124 |
Mask |= fcNegNormal | fcNegInf; |
4124 |
Mask |= fcNegNormal | fcNegInf; |
| 4125 |
|
4125 |
|
| 4126 |
break; |
4126 |
break; |
| 4127 |
} |
4127 |
} |
| 4128 |
case FCmpInst::FCMP_OGE: |
4128 |
case FCmpInst::FCMP_OGE: |
| 4129 |
case FCmpInst::FCMP_ULT: { |
4129 |
case FCmpInst::FCMP_ULT: { |
| 4130 |
// fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf |
4130 |
// fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf |
| 4131 |
// fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal |
4131 |
// fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal |
| 4132 |
// fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf) |
4132 |
// fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf) |
| 4133 |
// fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal) |
4133 |
// fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal) |
| 4134 |
Mask = fcPosInf | fcPosNormal; |
4134 |
Mask = fcPosInf | fcPosNormal; |
| 4135 |
if (IsFabs) |
4135 |
if (IsFabs) |
| 4136 |
Mask |= fcNegInf | fcNegNormal; |
4136 |
Mask |= fcNegInf | fcNegNormal; |
| 4137 |
break; |
4137 |
break; |
| 4138 |
} |
4138 |
} |
| 4139 |
default: |
4139 |
default: |
| 4140 |
return {nullptr, fcNone}; |
4140 |
return {nullptr, fcNone}; |
| 4141 |
} |
4141 |
} |
| 4142 |
} else if (ConstRHS->isNaN()) { |
4142 |
} else if (ConstRHS->isNaN()) { |
| 4143 |
// fcmp o__ x, nan -> false |
4143 |
// fcmp o__ x, nan -> false |
| 4144 |
// fcmp u__ x, nan -> true |
4144 |
// fcmp u__ x, nan -> true |
| 4145 |
Mask = fcNone; |
4145 |
Mask = fcNone; |
| 4146 |
} else |
4146 |
} else |
| 4147 |
return {nullptr, fcNone}; |
4147 |
return {nullptr, fcNone}; |
| 4148 |
|
4148 |
|
| 4149 |
// Invert the comparison for the unordered cases. |
4149 |
// Invert the comparison for the unordered cases. |
| 4150 |
if (FCmpInst::isUnordered(Pred)) |
4150 |
if (FCmpInst::isUnordered(Pred)) |
| 4151 |
Mask = ~Mask; |
4151 |
Mask = ~Mask; |
| 4152 |
|
4152 |
|
| 4153 |
return {Src, Mask}; |
4153 |
return {Src, Mask}; |
| 4154 |
} |
4154 |
} |
| 4155 |
|
4155 |
|
| 4156 |
static FPClassTest computeKnownFPClassFromAssumes(const Value *V, |
4156 |
static FPClassTest computeKnownFPClassFromAssumes(const Value *V, |
| 4157 |
const SimplifyQuery &Q) { |
4157 |
const SimplifyQuery &Q) { |
| 4158 |
FPClassTest KnownFromAssume = fcAllFlags; |
4158 |
FPClassTest KnownFromAssume = fcAllFlags; |
| 4159 |
|
4159 |
|
| 4160 |
// Try to restrict the floating-point classes based on information from |
4160 |
// Try to restrict the floating-point classes based on information from |
| 4161 |
// assumptions. |
4161 |
// assumptions. |
| 4162 |
for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { |
4162 |
for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { |
| 4163 |
if (!AssumeVH) |
4163 |
if (!AssumeVH) |
| 4164 |
continue; |
4164 |
continue; |
| 4165 |
CallInst *I = cast(AssumeVH); |
4165 |
CallInst *I = cast(AssumeVH); |
| 4166 |
const Function *F = I->getFunction(); |
4166 |
const Function *F = I->getFunction(); |
| 4167 |
|
4167 |
|
| 4168 |
assert(F == Q.CxtI->getParent()->getParent() && |
4168 |
assert(F == Q.CxtI->getParent()->getParent() && |
| 4169 |
"Got assumption for the wrong function!"); |
4169 |
"Got assumption for the wrong function!"); |
| 4170 |
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
4170 |
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
| 4171 |
"must be an assume intrinsic"); |
4171 |
"must be an assume intrinsic"); |
| 4172 |
|
4172 |
|
| 4173 |
if (!isValidAssumeForContext(I, Q.CxtI, Q.DT)) |
4173 |
if (!isValidAssumeForContext(I, Q.CxtI, Q.DT)) |
| 4174 |
continue; |
4174 |
continue; |
| 4175 |
|
4175 |
|
| 4176 |
CmpInst::Predicate Pred; |
4176 |
CmpInst::Predicate Pred; |
| 4177 |
Value *LHS, *RHS; |
4177 |
Value *LHS, *RHS; |
| 4178 |
uint64_t ClassVal = 0; |
4178 |
uint64_t ClassVal = 0; |
| 4179 |
if (match(I->getArgOperand(0), m_FCmp(Pred, m_Value(LHS), m_Value(RHS)))) { |
4179 |
if (match(I->getArgOperand(0), m_FCmp(Pred, m_Value(LHS), m_Value(RHS)))) { |
| 4180 |
auto [TestedValue, TestedMask] = |
4180 |
auto [TestedValue, TestedMask] = |
| 4181 |
fcmpToClassTest(Pred, *F, LHS, RHS, true); |
4181 |
fcmpToClassTest(Pred, *F, LHS, RHS, true); |
| 4182 |
// First see if we can fold in fabs/fneg into the test. |
4182 |
// First see if we can fold in fabs/fneg into the test. |
| 4183 |
if (TestedValue == V) |
4183 |
if (TestedValue == V) |
| 4184 |
KnownFromAssume &= TestedMask; |
4184 |
KnownFromAssume &= TestedMask; |
| 4185 |
else { |
4185 |
else { |
| 4186 |
// Try again without the lookthrough if we found a different source |
4186 |
// Try again without the lookthrough if we found a different source |
| 4187 |
// value. |
4187 |
// value. |
| 4188 |
auto [TestedValue, TestedMask] = |
4188 |
auto [TestedValue, TestedMask] = |
| 4189 |
fcmpToClassTest(Pred, *F, LHS, RHS, false); |
4189 |
fcmpToClassTest(Pred, *F, LHS, RHS, false); |
| 4190 |
if (TestedValue == V) |
4190 |
if (TestedValue == V) |
| 4191 |
KnownFromAssume &= TestedMask; |
4191 |
KnownFromAssume &= TestedMask; |
| 4192 |
} |
4192 |
} |
| 4193 |
} else if (match(I->getArgOperand(0), |
4193 |
} else if (match(I->getArgOperand(0), |
| 4194 |
m_Intrinsic( |
4194 |
m_Intrinsic( |
| 4195 |
m_Value(LHS), m_ConstantInt(ClassVal)))) { |
4195 |
m_Value(LHS), m_ConstantInt(ClassVal)))) { |
| 4196 |
KnownFromAssume &= static_cast(ClassVal); |
4196 |
KnownFromAssume &= static_cast(ClassVal); |
| 4197 |
} |
4197 |
} |
| 4198 |
} |
4198 |
} |
| 4199 |
|
4199 |
|
| 4200 |
return KnownFromAssume; |
4200 |
return KnownFromAssume; |
| 4201 |
} |
4201 |
} |
| 4202 |
|
4202 |
|
| 4203 |
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, |
4203 |
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, |
| 4204 |
FPClassTest InterestedClasses, KnownFPClass &Known, |
4204 |
FPClassTest InterestedClasses, KnownFPClass &Known, |
| 4205 |
unsigned Depth, const SimplifyQuery &Q); |
4205 |
unsigned Depth, const SimplifyQuery &Q); |
| 4206 |
|
4206 |
|
| 4207 |
static void computeKnownFPClass(const Value *V, KnownFPClass &Known, |
4207 |
static void computeKnownFPClass(const Value *V, KnownFPClass &Known, |
| 4208 |
FPClassTest InterestedClasses, unsigned Depth, |
4208 |
FPClassTest InterestedClasses, unsigned Depth, |
| 4209 |
const SimplifyQuery &Q) { |
4209 |
const SimplifyQuery &Q) { |
| 4210 |
auto *FVTy = dyn_cast(V->getType()); |
4210 |
auto *FVTy = dyn_cast(V->getType()); |
| 4211 |
APInt DemandedElts = |
4211 |
APInt DemandedElts = |
| 4212 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
4212 |
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); |
| 4213 |
computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Depth, Q); |
4213 |
computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Depth, Q); |
| 4214 |
} |
4214 |
} |
| 4215 |
|
4215 |
|
| 4216 |
static void computeKnownFPClassForFPTrunc(const Operator *Op, |
4216 |
static void computeKnownFPClassForFPTrunc(const Operator *Op, |
| 4217 |
const APInt &DemandedElts, |
4217 |
const APInt &DemandedElts, |
| 4218 |
FPClassTest InterestedClasses, |
4218 |
FPClassTest InterestedClasses, |
| 4219 |
KnownFPClass &Known, unsigned Depth, |
4219 |
KnownFPClass &Known, unsigned Depth, |
| 4220 |
const SimplifyQuery &Q) { |
4220 |
const SimplifyQuery &Q) { |
| 4221 |
if ((InterestedClasses & |
4221 |
if ((InterestedClasses & |
| 4222 |
(KnownFPClass::OrderedLessThanZeroMask | fcNan)) == fcNone) |
4222 |
(KnownFPClass::OrderedLessThanZeroMask | fcNan)) == fcNone) |
| 4223 |
return; |
4223 |
return; |
| 4224 |
|
4224 |
|
| 4225 |
KnownFPClass KnownSrc; |
4225 |
KnownFPClass KnownSrc; |
| 4226 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses, |
4226 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses, |
| 4227 |
KnownSrc, Depth + 1, Q); |
4227 |
KnownSrc, Depth + 1, Q); |
| 4228 |
|
4228 |
|
| 4229 |
// Sign should be preserved |
4229 |
// Sign should be preserved |
| 4230 |
// TODO: Handle cannot be ordered greater than zero |
4230 |
// TODO: Handle cannot be ordered greater than zero |
| 4231 |
if (KnownSrc.cannotBeOrderedLessThanZero()) |
4231 |
if (KnownSrc.cannotBeOrderedLessThanZero()) |
| 4232 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
4232 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
| 4233 |
|
4233 |
|
| 4234 |
Known.propagateNaN(KnownSrc, true); |
4234 |
Known.propagateNaN(KnownSrc, true); |
| 4235 |
|
4235 |
|
| 4236 |
// Infinity needs a range check. |
4236 |
// Infinity needs a range check. |
| 4237 |
} |
4237 |
} |
| 4238 |
|
4238 |
|
| 4239 |
// TODO: Merge implementation of cannotBeOrderedLessThanZero into here. |
4239 |
// TODO: Merge implementation of cannotBeOrderedLessThanZero into here. |
| 4240 |
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, |
4240 |
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, |
| 4241 |
FPClassTest InterestedClasses, KnownFPClass &Known, |
4241 |
FPClassTest InterestedClasses, KnownFPClass &Known, |
| 4242 |
unsigned Depth, const SimplifyQuery &Q) { |
4242 |
unsigned Depth, const SimplifyQuery &Q) { |
| 4243 |
assert(Known.isUnknown() && "should not be called with known information"); |
4243 |
assert(Known.isUnknown() && "should not be called with known information"); |
| 4244 |
|
4244 |
|
| 4245 |
if (!DemandedElts) { |
4245 |
if (!DemandedElts) { |
| 4246 |
// No demanded elts, better to assume we don't know anything. |
4246 |
// No demanded elts, better to assume we don't know anything. |
| 4247 |
Known.resetAll(); |
4247 |
Known.resetAll(); |
| 4248 |
return; |
4248 |
return; |
| 4249 |
} |
4249 |
} |
| 4250 |
|
4250 |
|
| 4251 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
4251 |
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); |
| 4252 |
|
4252 |
|
| 4253 |
if (auto *CFP = dyn_cast_or_null(V)) { |
4253 |
if (auto *CFP = dyn_cast_or_null(V)) { |
| 4254 |
Known.KnownFPClasses = CFP->getValueAPF().classify(); |
4254 |
Known.KnownFPClasses = CFP->getValueAPF().classify(); |
| 4255 |
Known.SignBit = CFP->isNegative(); |
4255 |
Known.SignBit = CFP->isNegative(); |
| 4256 |
return; |
4256 |
return; |
| 4257 |
} |
4257 |
} |
| 4258 |
|
4258 |
|
| 4259 |
// Try to handle fixed width vector constants |
4259 |
// Try to handle fixed width vector constants |
| 4260 |
auto *VFVTy = dyn_cast(V->getType()); |
4260 |
auto *VFVTy = dyn_cast(V->getType()); |
| 4261 |
const Constant *CV = dyn_cast(V); |
4261 |
const Constant *CV = dyn_cast(V); |
| 4262 |
if (VFVTy && CV) { |
4262 |
if (VFVTy && CV) { |
| 4263 |
Known.KnownFPClasses = fcNone; |
4263 |
Known.KnownFPClasses = fcNone; |
| 4264 |
|
4264 |
|
| 4265 |
// For vectors, verify that each element is not NaN. |
4265 |
// For vectors, verify that each element is not NaN. |
| 4266 |
unsigned NumElts = VFVTy->getNumElements(); |
4266 |
unsigned NumElts = VFVTy->getNumElements(); |
| 4267 |
for (unsigned i = 0; i != NumElts; ++i) { |
4267 |
for (unsigned i = 0; i != NumElts; ++i) { |
| 4268 |
Constant *Elt = CV->getAggregateElement(i); |
4268 |
Constant *Elt = CV->getAggregateElement(i); |
| 4269 |
if (!Elt) { |
4269 |
if (!Elt) { |
| 4270 |
Known = KnownFPClass(); |
4270 |
Known = KnownFPClass(); |
| 4271 |
return; |
4271 |
return; |
| 4272 |
} |
4272 |
} |
| 4273 |
if (isa(Elt)) |
4273 |
if (isa(Elt)) |
| 4274 |
continue; |
4274 |
continue; |
| 4275 |
auto *CElt = dyn_cast(Elt); |
4275 |
auto *CElt = dyn_cast(Elt); |
| 4276 |
if (!CElt) { |
4276 |
if (!CElt) { |
| 4277 |
Known = KnownFPClass(); |
4277 |
Known = KnownFPClass(); |
| 4278 |
return; |
4278 |
return; |
| 4279 |
} |
4279 |
} |
| 4280 |
|
4280 |
|
| 4281 |
KnownFPClass KnownElt{CElt->getValueAPF().classify(), CElt->isNegative()}; |
4281 |
KnownFPClass KnownElt{CElt->getValueAPF().classify(), CElt->isNegative()}; |
| 4282 |
Known |= KnownElt; |
4282 |
Known |= KnownElt; |
| 4283 |
} |
4283 |
} |
| 4284 |
|
4284 |
|
| 4285 |
return; |
4285 |
return; |
| 4286 |
} |
4286 |
} |
| 4287 |
|
4287 |
|
| 4288 |
FPClassTest KnownNotFromFlags = fcNone; |
4288 |
FPClassTest KnownNotFromFlags = fcNone; |
| 4289 |
if (const auto *CB = dyn_cast(V)) |
4289 |
if (const auto *CB = dyn_cast(V)) |
| 4290 |
KnownNotFromFlags |= CB->getRetNoFPClass(); |
4290 |
KnownNotFromFlags |= CB->getRetNoFPClass(); |
| 4291 |
else if (const auto *Arg = dyn_cast(V)) |
4291 |
else if (const auto *Arg = dyn_cast(V)) |
| 4292 |
KnownNotFromFlags |= Arg->getNoFPClass(); |
4292 |
KnownNotFromFlags |= Arg->getNoFPClass(); |
| 4293 |
|
4293 |
|
| 4294 |
const Operator *Op = dyn_cast(V); |
4294 |
const Operator *Op = dyn_cast(V); |
| 4295 |
if (const FPMathOperator *FPOp = dyn_cast_or_null(Op)) { |
4295 |
if (const FPMathOperator *FPOp = dyn_cast_or_null(Op)) { |
| 4296 |
if (FPOp->hasNoNaNs()) |
4296 |
if (FPOp->hasNoNaNs()) |
| 4297 |
KnownNotFromFlags |= fcNan; |
4297 |
KnownNotFromFlags |= fcNan; |
| 4298 |
if (FPOp->hasNoInfs()) |
4298 |
if (FPOp->hasNoInfs()) |
| 4299 |
KnownNotFromFlags |= fcInf; |
4299 |
KnownNotFromFlags |= fcInf; |
| 4300 |
} |
4300 |
} |
| 4301 |
|
4301 |
|
| 4302 |
if (Q.AC) { |
4302 |
if (Q.AC) { |
| 4303 |
FPClassTest AssumedClasses = computeKnownFPClassFromAssumes(V, Q); |
4303 |
FPClassTest AssumedClasses = computeKnownFPClassFromAssumes(V, Q); |
| 4304 |
KnownNotFromFlags |= ~AssumedClasses; |
4304 |
KnownNotFromFlags |= ~AssumedClasses; |
| 4305 |
} |
4305 |
} |
| 4306 |
|
4306 |
|
| 4307 |
// We no longer need to find out about these bits from inputs if we can |
4307 |
// We no longer need to find out about these bits from inputs if we can |
| 4308 |
// assume this from flags/attributes. |
4308 |
// assume this from flags/attributes. |
| 4309 |
InterestedClasses &= ~KnownNotFromFlags; |
4309 |
InterestedClasses &= ~KnownNotFromFlags; |
| 4310 |
|
4310 |
|
| 4311 |
auto ClearClassesFromFlags = make_scope_exit([=, &Known] { |
4311 |
auto ClearClassesFromFlags = make_scope_exit([=, &Known] { |
| 4312 |
Known.knownNot(KnownNotFromFlags); |
4312 |
Known.knownNot(KnownNotFromFlags); |
| 4313 |
}); |
4313 |
}); |
| 4314 |
|
4314 |
|
| 4315 |
if (!Op) |
4315 |
if (!Op) |
| 4316 |
return; |
4316 |
return; |
| 4317 |
|
4317 |
|
| 4318 |
// All recursive calls that increase depth must come after this. |
4318 |
// All recursive calls that increase depth must come after this. |
| 4319 |
if (Depth == MaxAnalysisRecursionDepth) |
4319 |
if (Depth == MaxAnalysisRecursionDepth) |
| 4320 |
return; |
4320 |
return; |
| 4321 |
|
4321 |
|
| 4322 |
const unsigned Opc = Op->getOpcode(); |
4322 |
const unsigned Opc = Op->getOpcode(); |
| 4323 |
switch (Opc) { |
4323 |
switch (Opc) { |
| 4324 |
case Instruction::FNeg: { |
4324 |
case Instruction::FNeg: { |
| 4325 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses, |
4325 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses, |
| 4326 |
Known, Depth + 1, Q); |
4326 |
Known, Depth + 1, Q); |
| 4327 |
Known.fneg(); |
4327 |
Known.fneg(); |
| 4328 |
break; |
4328 |
break; |
| 4329 |
} |
4329 |
} |
| 4330 |
case Instruction::Select: { |
4330 |
case Instruction::Select: { |
| 4331 |
Value *Cond = Op->getOperand(0); |
4331 |
Value *Cond = Op->getOperand(0); |
| 4332 |
Value *LHS = Op->getOperand(1); |
4332 |
Value *LHS = Op->getOperand(1); |
| 4333 |
Value *RHS = Op->getOperand(2); |
4333 |
Value *RHS = Op->getOperand(2); |
| 4334 |
|
4334 |
|
| 4335 |
FPClassTest FilterLHS = fcAllFlags; |
4335 |
FPClassTest FilterLHS = fcAllFlags; |
| 4336 |
FPClassTest FilterRHS = fcAllFlags; |
4336 |
FPClassTest FilterRHS = fcAllFlags; |
| 4337 |
|
4337 |
|
| 4338 |
Value *TestedValue = nullptr; |
4338 |
Value *TestedValue = nullptr; |
| 4339 |
FPClassTest TestedMask = fcNone; |
4339 |
FPClassTest TestedMask = fcNone; |
| 4340 |
uint64_t ClassVal = 0; |
4340 |
uint64_t ClassVal = 0; |
| 4341 |
const Function *F = cast(Op)->getFunction(); |
4341 |
const Function *F = cast(Op)->getFunction(); |
| 4342 |
CmpInst::Predicate Pred; |
4342 |
CmpInst::Predicate Pred; |
| 4343 |
Value *CmpLHS, *CmpRHS; |
4343 |
Value *CmpLHS, *CmpRHS; |
| 4344 |
if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) { |
4344 |
if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) { |
| 4345 |
// If the select filters out a value based on the class, it no longer |
4345 |
// If the select filters out a value based on the class, it no longer |
| 4346 |
// participates in the class of the result |
4346 |
// participates in the class of the result |
| 4347 |
|
4347 |
|
| 4348 |
// TODO: In some degenerate cases we can infer something if we try again |
4348 |
// TODO: In some degenerate cases we can infer something if we try again |
| 4349 |
// without looking through sign operations. |
4349 |
// without looking through sign operations. |
| 4350 |
bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS; |
4350 |
bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS; |
| 4351 |
std::tie(TestedValue, TestedMask) = |
4351 |
std::tie(TestedValue, TestedMask) = |
| 4352 |
fcmpToClassTest(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg); |
4352 |
fcmpToClassTest(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg); |
| 4353 |
} else if (match(Cond, |
4353 |
} else if (match(Cond, |
| 4354 |
m_Intrinsic( |
4354 |
m_Intrinsic( |
| 4355 |
m_Value(TestedValue), m_ConstantInt(ClassVal)))) { |
4355 |
m_Value(TestedValue), m_ConstantInt(ClassVal)))) { |
| 4356 |
TestedMask = static_cast(ClassVal); |
4356 |
TestedMask = static_cast(ClassVal); |
| 4357 |
} |
4357 |
} |
| 4358 |
|
4358 |
|
| 4359 |
if (TestedValue == LHS) { |
4359 |
if (TestedValue == LHS) { |
| 4360 |
// match !isnan(x) ? x : y |
4360 |
// match !isnan(x) ? x : y |
| 4361 |
FilterLHS = TestedMask; |
4361 |
FilterLHS = TestedMask; |
| 4362 |
} else if (TestedValue == RHS) { |
4362 |
} else if (TestedValue == RHS) { |
| 4363 |
// match !isnan(x) ? y : x |
4363 |
// match !isnan(x) ? y : x |
| 4364 |
FilterRHS = ~TestedMask; |
4364 |
FilterRHS = ~TestedMask; |
| 4365 |
} |
4365 |
} |
| 4366 |
|
4366 |
|
| 4367 |
KnownFPClass Known2; |
4367 |
KnownFPClass Known2; |
| 4368 |
computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known, |
4368 |
computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known, |
| 4369 |
Depth + 1, Q); |
4369 |
Depth + 1, Q); |
| 4370 |
Known.KnownFPClasses &= FilterLHS; |
4370 |
Known.KnownFPClasses &= FilterLHS; |
| 4371 |
|
4371 |
|
| 4372 |
computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS, |
4372 |
computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS, |
| 4373 |
Known2, Depth + 1, Q); |
4373 |
Known2, Depth + 1, Q); |
| 4374 |
Known2.KnownFPClasses &= FilterRHS; |
4374 |
Known2.KnownFPClasses &= FilterRHS; |
| 4375 |
|
4375 |
|
| 4376 |
Known |= Known2; |
4376 |
Known |= Known2; |
| 4377 |
break; |
4377 |
break; |
| 4378 |
} |
4378 |
} |
| 4379 |
case Instruction::Call: { |
4379 |
case Instruction::Call: { |
| 4380 |
if (const IntrinsicInst *II = dyn_cast(Op)) { |
4380 |
if (const IntrinsicInst *II = dyn_cast(Op)) { |
| 4381 |
const Intrinsic::ID IID = II->getIntrinsicID(); |
4381 |
const Intrinsic::ID IID = II->getIntrinsicID(); |
| 4382 |
switch (IID) { |
4382 |
switch (IID) { |
| 4383 |
case Intrinsic::fabs: { |
4383 |
case Intrinsic::fabs: { |
| 4384 |
if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) { |
4384 |
if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) { |
| 4385 |
// If we only care about the sign bit we don't need to inspect the |
4385 |
// If we only care about the sign bit we don't need to inspect the |
| 4386 |
// operand. |
4386 |
// operand. |
| 4387 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4387 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4388 |
InterestedClasses, Known, Depth + 1, Q); |
4388 |
InterestedClasses, Known, Depth + 1, Q); |
| 4389 |
} |
4389 |
} |
| 4390 |
|
4390 |
|
| 4391 |
Known.fabs(); |
4391 |
Known.fabs(); |
| 4392 |
break; |
4392 |
break; |
| 4393 |
} |
4393 |
} |
| 4394 |
case Intrinsic::copysign: { |
4394 |
case Intrinsic::copysign: { |
| 4395 |
KnownFPClass KnownSign; |
4395 |
KnownFPClass KnownSign; |
| 4396 |
|
4396 |
|
| 4397 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4397 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4398 |
InterestedClasses, Known, Depth + 1, Q); |
4398 |
InterestedClasses, Known, Depth + 1, Q); |
| 4399 |
computeKnownFPClass(II->getArgOperand(1), DemandedElts, |
4399 |
computeKnownFPClass(II->getArgOperand(1), DemandedElts, |
| 4400 |
InterestedClasses, KnownSign, Depth + 1, Q); |
4400 |
InterestedClasses, KnownSign, Depth + 1, Q); |
| 4401 |
Known.copysign(KnownSign); |
4401 |
Known.copysign(KnownSign); |
| 4402 |
break; |
4402 |
break; |
| 4403 |
} |
4403 |
} |
| 4404 |
case Intrinsic::fma: |
4404 |
case Intrinsic::fma: |
| 4405 |
case Intrinsic::fmuladd: { |
4405 |
case Intrinsic::fmuladd: { |
| 4406 |
if ((InterestedClasses & fcNegative) == fcNone) |
4406 |
if ((InterestedClasses & fcNegative) == fcNone) |
| 4407 |
break; |
4407 |
break; |
| 4408 |
|
4408 |
|
| 4409 |
if (II->getArgOperand(0) != II->getArgOperand(1)) |
4409 |
if (II->getArgOperand(0) != II->getArgOperand(1)) |
| 4410 |
break; |
4410 |
break; |
| 4411 |
|
4411 |
|
| 4412 |
// The multiply cannot be -0 and therefore the add can't be -0 |
4412 |
// The multiply cannot be -0 and therefore the add can't be -0 |
| 4413 |
Known.knownNot(fcNegZero); |
4413 |
Known.knownNot(fcNegZero); |
| 4414 |
|
4414 |
|
| 4415 |
// x * x + y is non-negative if y is non-negative. |
4415 |
// x * x + y is non-negative if y is non-negative. |
| 4416 |
KnownFPClass KnownAddend; |
4416 |
KnownFPClass KnownAddend; |
| 4417 |
computeKnownFPClass(II->getArgOperand(2), DemandedElts, |
4417 |
computeKnownFPClass(II->getArgOperand(2), DemandedElts, |
| 4418 |
InterestedClasses, KnownAddend, Depth + 1, Q); |
4418 |
InterestedClasses, KnownAddend, Depth + 1, Q); |
| 4419 |
|
4419 |
|
| 4420 |
// TODO: Known sign bit with no nans |
4420 |
// TODO: Known sign bit with no nans |
| 4421 |
if (KnownAddend.cannotBeOrderedLessThanZero()) |
4421 |
if (KnownAddend.cannotBeOrderedLessThanZero()) |
| 4422 |
Known.knownNot(fcNegative); |
4422 |
Known.knownNot(fcNegative); |
| 4423 |
break; |
4423 |
break; |
| 4424 |
} |
4424 |
} |
| 4425 |
case Intrinsic::sqrt: |
4425 |
case Intrinsic::sqrt: |
| 4426 |
case Intrinsic::experimental_constrained_sqrt: { |
4426 |
case Intrinsic::experimental_constrained_sqrt: { |
| 4427 |
KnownFPClass KnownSrc; |
4427 |
KnownFPClass KnownSrc; |
| 4428 |
FPClassTest InterestedSrcs = InterestedClasses; |
4428 |
FPClassTest InterestedSrcs = InterestedClasses; |
| 4429 |
if (InterestedClasses & fcNan) |
4429 |
if (InterestedClasses & fcNan) |
| 4430 |
InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask; |
4430 |
InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask; |
| 4431 |
|
4431 |
|
| 4432 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4432 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4433 |
InterestedSrcs, KnownSrc, Depth + 1, Q); |
4433 |
InterestedSrcs, KnownSrc, Depth + 1, Q); |
| 4434 |
|
4434 |
|
| 4435 |
if (KnownSrc.isKnownNeverPosInfinity()) |
4435 |
if (KnownSrc.isKnownNeverPosInfinity()) |
| 4436 |
Known.knownNot(fcPosInf); |
4436 |
Known.knownNot(fcPosInf); |
| 4437 |
if (KnownSrc.isKnownNever(fcSNan)) |
4437 |
if (KnownSrc.isKnownNever(fcSNan)) |
| 4438 |
Known.knownNot(fcSNan); |
4438 |
Known.knownNot(fcSNan); |
| 4439 |
|
4439 |
|
| 4440 |
// Any negative value besides -0 returns a nan. |
4440 |
// Any negative value besides -0 returns a nan. |
| 4441 |
if (KnownSrc.isKnownNeverNaN() && |
4441 |
if (KnownSrc.isKnownNeverNaN() && |
| 4442 |
KnownSrc.cannotBeOrderedLessThanZero()) |
4442 |
KnownSrc.cannotBeOrderedLessThanZero()) |
| 4443 |
Known.knownNot(fcNan); |
4443 |
Known.knownNot(fcNan); |
| 4444 |
|
4444 |
|
| 4445 |
// The only negative value that can be returned is -0 for -0 inputs. |
4445 |
// The only negative value that can be returned is -0 for -0 inputs. |
| 4446 |
Known.knownNot(fcNegInf | fcNegSubnormal | fcNegNormal); |
4446 |
Known.knownNot(fcNegInf | fcNegSubnormal | fcNegNormal); |
| 4447 |
|
4447 |
|
| 4448 |
// If the input denormal mode could be PreserveSign, a negative |
4448 |
// If the input denormal mode could be PreserveSign, a negative |
| 4449 |
// subnormal input could produce a negative zero output. |
4449 |
// subnormal input could produce a negative zero output. |
| 4450 |
const Function *F = II->getFunction(); |
4450 |
const Function *F = II->getFunction(); |
| 4451 |
if (Q.IIQ.hasNoSignedZeros(II) || |
4451 |
if (Q.IIQ.hasNoSignedZeros(II) || |
| 4452 |
(F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))) { |
4452 |
(F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))) { |
| 4453 |
Known.knownNot(fcNegZero); |
4453 |
Known.knownNot(fcNegZero); |
| 4454 |
if (KnownSrc.isKnownNeverNaN()) |
4454 |
if (KnownSrc.isKnownNeverNaN()) |
| 4455 |
Known.SignBit = false; |
4455 |
Known.SignBit = false; |
| 4456 |
} |
4456 |
} |
| 4457 |
|
4457 |
|
| 4458 |
break; |
4458 |
break; |
| 4459 |
} |
4459 |
} |
| 4460 |
case Intrinsic::sin: |
4460 |
case Intrinsic::sin: |
| 4461 |
case Intrinsic::cos: { |
4461 |
case Intrinsic::cos: { |
| 4462 |
// Return NaN on infinite inputs. |
4462 |
// Return NaN on infinite inputs. |
| 4463 |
KnownFPClass KnownSrc; |
4463 |
KnownFPClass KnownSrc; |
| 4464 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4464 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4465 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
4465 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
| 4466 |
Known.knownNot(fcInf); |
4466 |
Known.knownNot(fcInf); |
| 4467 |
if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity()) |
4467 |
if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity()) |
| 4468 |
Known.knownNot(fcNan); |
4468 |
Known.knownNot(fcNan); |
| 4469 |
break; |
4469 |
break; |
| 4470 |
} |
4470 |
} |
| 4471 |
|
4471 |
|
| 4472 |
case Intrinsic::maxnum: |
4472 |
case Intrinsic::maxnum: |
| 4473 |
case Intrinsic::minnum: |
4473 |
case Intrinsic::minnum: |
| 4474 |
case Intrinsic::minimum: |
4474 |
case Intrinsic::minimum: |
| 4475 |
case Intrinsic::maximum: { |
4475 |
case Intrinsic::maximum: { |
| 4476 |
KnownFPClass KnownLHS, KnownRHS; |
4476 |
KnownFPClass KnownLHS, KnownRHS; |
| 4477 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4477 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4478 |
InterestedClasses, KnownLHS, Depth + 1, Q); |
4478 |
InterestedClasses, KnownLHS, Depth + 1, Q); |
| 4479 |
computeKnownFPClass(II->getArgOperand(1), DemandedElts, |
4479 |
computeKnownFPClass(II->getArgOperand(1), DemandedElts, |
| 4480 |
InterestedClasses, KnownRHS, Depth + 1, Q); |
4480 |
InterestedClasses, KnownRHS, Depth + 1, Q); |
| 4481 |
|
4481 |
|
| 4482 |
bool NeverNaN = |
4482 |
bool NeverNaN = |
| 4483 |
KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN(); |
4483 |
KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN(); |
| 4484 |
Known = KnownLHS | KnownRHS; |
4484 |
Known = KnownLHS | KnownRHS; |
| 4485 |
|
4485 |
|
| 4486 |
// If either operand is not NaN, the result is not NaN. |
4486 |
// If either operand is not NaN, the result is not NaN. |
| 4487 |
if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum)) |
4487 |
if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum)) |
| 4488 |
Known.knownNot(fcNan); |
4488 |
Known.knownNot(fcNan); |
| 4489 |
|
4489 |
|
| 4490 |
if (IID == Intrinsic::maxnum) { |
4490 |
if (IID == Intrinsic::maxnum) { |
| 4491 |
// If at least one operand is known to be positive, the result must be |
4491 |
// If at least one operand is known to be positive, the result must be |
| 4492 |
// positive. |
4492 |
// positive. |
| 4493 |
if ((KnownLHS.cannotBeOrderedLessThanZero() && |
4493 |
if ((KnownLHS.cannotBeOrderedLessThanZero() && |
| 4494 |
KnownLHS.isKnownNeverNaN()) || |
4494 |
KnownLHS.isKnownNeverNaN()) || |
| 4495 |
(KnownRHS.cannotBeOrderedLessThanZero() && |
4495 |
(KnownRHS.cannotBeOrderedLessThanZero() && |
| 4496 |
KnownRHS.isKnownNeverNaN())) |
4496 |
KnownRHS.isKnownNeverNaN())) |
| 4497 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
4497 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
| 4498 |
} else if (IID == Intrinsic::maximum) { |
4498 |
} else if (IID == Intrinsic::maximum) { |
| 4499 |
// If at least one operand is known to be positive, the result must be |
4499 |
// If at least one operand is known to be positive, the result must be |
| 4500 |
// positive. |
4500 |
// positive. |
| 4501 |
if (KnownLHS.cannotBeOrderedLessThanZero() || |
4501 |
if (KnownLHS.cannotBeOrderedLessThanZero() || |
| 4502 |
KnownRHS.cannotBeOrderedLessThanZero()) |
4502 |
KnownRHS.cannotBeOrderedLessThanZero()) |
| 4503 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
4503 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
| 4504 |
} else if (IID == Intrinsic::minnum) { |
4504 |
} else if (IID == Intrinsic::minnum) { |
| 4505 |
// If at least one operand is known to be negative, the result must be |
4505 |
// If at least one operand is known to be negative, the result must be |
| 4506 |
// negative. |
4506 |
// negative. |
| 4507 |
if ((KnownLHS.cannotBeOrderedGreaterThanZero() && |
4507 |
if ((KnownLHS.cannotBeOrderedGreaterThanZero() && |
| 4508 |
KnownLHS.isKnownNeverNaN()) || |
4508 |
KnownLHS.isKnownNeverNaN()) || |
| 4509 |
(KnownRHS.cannotBeOrderedGreaterThanZero() && |
4509 |
(KnownRHS.cannotBeOrderedGreaterThanZero() && |
| 4510 |
KnownRHS.isKnownNeverNaN())) |
4510 |
KnownRHS.isKnownNeverNaN())) |
| 4511 |
Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask); |
4511 |
Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask); |
| 4512 |
} else { |
4512 |
} else { |
| 4513 |
// If at least one operand is known to be negative, the result must be |
4513 |
// If at least one operand is known to be negative, the result must be |
| 4514 |
// negative. |
4514 |
// negative. |
| 4515 |
if (KnownLHS.cannotBeOrderedGreaterThanZero() || |
4515 |
if (KnownLHS.cannotBeOrderedGreaterThanZero() || |
| 4516 |
KnownRHS.cannotBeOrderedGreaterThanZero()) |
4516 |
KnownRHS.cannotBeOrderedGreaterThanZero()) |
| 4517 |
Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask); |
4517 |
Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask); |
| 4518 |
} |
4518 |
} |
| 4519 |
|
4519 |
|
| 4520 |
// Fixup zero handling if denormals could be returned as a zero. |
4520 |
// Fixup zero handling if denormals could be returned as a zero. |
| 4521 |
// |
4521 |
// |
| 4522 |
// As there's no spec for denormal flushing, be conservative with the |
4522 |
// As there's no spec for denormal flushing, be conservative with the |
| 4523 |
// treatment of denormals that could be flushed to zero. For older |
4523 |
// treatment of denormals that could be flushed to zero. For older |
| 4524 |
// subtargets on AMDGPU the min/max instructions would not flush the |
4524 |
// subtargets on AMDGPU the min/max instructions would not flush the |
| 4525 |
// output and return the original value. |
4525 |
// output and return the original value. |
| 4526 |
// |
4526 |
// |
| 4527 |
// TODO: This could be refined based on the sign |
4527 |
// TODO: This could be refined based on the sign |
| 4528 |
if ((Known.KnownFPClasses & fcZero) != fcNone && |
4528 |
if ((Known.KnownFPClasses & fcZero) != fcNone && |
| 4529 |
!Known.isKnownNeverSubnormal()) { |
4529 |
!Known.isKnownNeverSubnormal()) { |
| 4530 |
const Function *Parent = II->getFunction(); |
4530 |
const Function *Parent = II->getFunction(); |
| 4531 |
if (!Parent) |
4531 |
if (!Parent) |
| 4532 |
break; |
4532 |
break; |
| 4533 |
|
4533 |
|
| 4534 |
DenormalMode Mode = Parent->getDenormalMode( |
4534 |
DenormalMode Mode = Parent->getDenormalMode( |
| 4535 |
II->getType()->getScalarType()->getFltSemantics()); |
4535 |
II->getType()->getScalarType()->getFltSemantics()); |
| 4536 |
if (Mode != DenormalMode::getIEEE()) |
4536 |
if (Mode != DenormalMode::getIEEE()) |
| 4537 |
Known.KnownFPClasses |= fcZero; |
4537 |
Known.KnownFPClasses |= fcZero; |
| 4538 |
} |
4538 |
} |
| 4539 |
|
4539 |
|
| 4540 |
break; |
4540 |
break; |
| 4541 |
} |
4541 |
} |
| 4542 |
case Intrinsic::canonicalize: { |
4542 |
case Intrinsic::canonicalize: { |
| 4543 |
KnownFPClass KnownSrc; |
4543 |
KnownFPClass KnownSrc; |
| 4544 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4544 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4545 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
4545 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
| 4546 |
|
4546 |
|
| 4547 |
// This is essentially a stronger form of |
4547 |
// This is essentially a stronger form of |
| 4548 |
// propagateCanonicalizingSrc. Other "canonicalizing" operations don't |
4548 |
// propagateCanonicalizingSrc. Other "canonicalizing" operations don't |
| 4549 |
// actually have an IR canonicalization guarantee. |
4549 |
// actually have an IR canonicalization guarantee. |
| 4550 |
|
4550 |
|
| 4551 |
// Canonicalize may flush denormals to zero, so we have to consider the |
4551 |
// Canonicalize may flush denormals to zero, so we have to consider the |
| 4552 |
// denormal mode to preserve known-not-0 knowledge. |
4552 |
// denormal mode to preserve known-not-0 knowledge. |
| 4553 |
Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan; |
4553 |
Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan; |
| 4554 |
|
4554 |
|
| 4555 |
// Stronger version of propagateNaN |
4555 |
// Stronger version of propagateNaN |
| 4556 |
// Canonicalize is guaranteed to quiet signaling nans. |
4556 |
// Canonicalize is guaranteed to quiet signaling nans. |
| 4557 |
if (KnownSrc.isKnownNeverNaN()) |
4557 |
if (KnownSrc.isKnownNeverNaN()) |
| 4558 |
Known.knownNot(fcNan); |
4558 |
Known.knownNot(fcNan); |
| 4559 |
else |
4559 |
else |
| 4560 |
Known.knownNot(fcSNan); |
4560 |
Known.knownNot(fcSNan); |
| 4561 |
|
4561 |
|
| 4562 |
const Function *F = II->getFunction(); |
4562 |
const Function *F = II->getFunction(); |
| 4563 |
if (!F) |
4563 |
if (!F) |
| 4564 |
break; |
4564 |
break; |
| 4565 |
|
4565 |
|
| 4566 |
// If the parent function flushes denormals, the canonical output cannot |
4566 |
// If the parent function flushes denormals, the canonical output cannot |
| 4567 |
// be a denormal. |
4567 |
// be a denormal. |
| 4568 |
const fltSemantics &FPType = |
4568 |
const fltSemantics &FPType = |
| 4569 |
II->getType()->getScalarType()->getFltSemantics(); |
4569 |
II->getType()->getScalarType()->getFltSemantics(); |
| 4570 |
DenormalMode DenormMode = F->getDenormalMode(FPType); |
4570 |
DenormalMode DenormMode = F->getDenormalMode(FPType); |
| 4571 |
if (DenormMode == DenormalMode::getIEEE()) { |
4571 |
if (DenormMode == DenormalMode::getIEEE()) { |
| 4572 |
if (KnownSrc.isKnownNever(fcPosZero)) |
4572 |
if (KnownSrc.isKnownNever(fcPosZero)) |
| 4573 |
Known.knownNot(fcPosZero); |
4573 |
Known.knownNot(fcPosZero); |
| 4574 |
if (KnownSrc.isKnownNever(fcNegZero)) |
4574 |
if (KnownSrc.isKnownNever(fcNegZero)) |
| 4575 |
Known.knownNot(fcNegZero); |
4575 |
Known.knownNot(fcNegZero); |
| 4576 |
break; |
4576 |
break; |
| 4577 |
} |
4577 |
} |
| 4578 |
|
4578 |
|
| 4579 |
if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero()) |
4579 |
if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero()) |
| 4580 |
Known.knownNot(fcSubnormal); |
4580 |
Known.knownNot(fcSubnormal); |
| 4581 |
|
4581 |
|
| 4582 |
if (DenormMode.Input == DenormalMode::PositiveZero || |
4582 |
if (DenormMode.Input == DenormalMode::PositiveZero || |
| 4583 |
(DenormMode.Output == DenormalMode::PositiveZero && |
4583 |
(DenormMode.Output == DenormalMode::PositiveZero && |
| 4584 |
DenormMode.Input == DenormalMode::IEEE)) |
4584 |
DenormMode.Input == DenormalMode::IEEE)) |
| 4585 |
Known.knownNot(fcNegZero); |
4585 |
Known.knownNot(fcNegZero); |
| 4586 |
|
4586 |
|
| 4587 |
break; |
4587 |
break; |
| 4588 |
} |
4588 |
} |
| 4589 |
case Intrinsic::trunc: |
4589 |
case Intrinsic::trunc: |
| 4590 |
case Intrinsic::floor: |
4590 |
case Intrinsic::floor: |
| 4591 |
case Intrinsic::ceil: |
4591 |
case Intrinsic::ceil: |
| 4592 |
case Intrinsic::rint: |
4592 |
case Intrinsic::rint: |
| 4593 |
case Intrinsic::nearbyint: |
4593 |
case Intrinsic::nearbyint: |
| 4594 |
case Intrinsic::round: |
4594 |
case Intrinsic::round: |
| 4595 |
case Intrinsic::roundeven: { |
4595 |
case Intrinsic::roundeven: { |
| 4596 |
KnownFPClass KnownSrc; |
4596 |
KnownFPClass KnownSrc; |
| 4597 |
FPClassTest InterestedSrcs = InterestedClasses; |
4597 |
FPClassTest InterestedSrcs = InterestedClasses; |
| 4598 |
if (InterestedSrcs & fcPosFinite) |
4598 |
if (InterestedSrcs & fcPosFinite) |
| 4599 |
InterestedSrcs |= fcPosFinite; |
4599 |
InterestedSrcs |= fcPosFinite; |
| 4600 |
if (InterestedSrcs & fcNegFinite) |
4600 |
if (InterestedSrcs & fcNegFinite) |
| 4601 |
InterestedSrcs |= fcNegFinite; |
4601 |
InterestedSrcs |= fcNegFinite; |
| 4602 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4602 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4603 |
InterestedSrcs, KnownSrc, Depth + 1, Q); |
4603 |
InterestedSrcs, KnownSrc, Depth + 1, Q); |
| 4604 |
|
4604 |
|
| 4605 |
// Integer results cannot be subnormal. |
4605 |
// Integer results cannot be subnormal. |
| 4606 |
Known.knownNot(fcSubnormal); |
4606 |
Known.knownNot(fcSubnormal); |
| 4607 |
|
4607 |
|
| 4608 |
Known.propagateNaN(KnownSrc, true); |
4608 |
Known.propagateNaN(KnownSrc, true); |
| 4609 |
|
4609 |
|
| 4610 |
// Pass through infinities, except PPC_FP128 is a special case for |
4610 |
// Pass through infinities, except PPC_FP128 is a special case for |
| 4611 |
// intrinsics other than trunc. |
4611 |
// intrinsics other than trunc. |
| 4612 |
if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) { |
4612 |
if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) { |
| 4613 |
if (KnownSrc.isKnownNeverPosInfinity()) |
4613 |
if (KnownSrc.isKnownNeverPosInfinity()) |
| 4614 |
Known.knownNot(fcPosInf); |
4614 |
Known.knownNot(fcPosInf); |
| 4615 |
if (KnownSrc.isKnownNeverNegInfinity()) |
4615 |
if (KnownSrc.isKnownNeverNegInfinity()) |
| 4616 |
Known.knownNot(fcNegInf); |
4616 |
Known.knownNot(fcNegInf); |
| 4617 |
} |
4617 |
} |
| 4618 |
|
4618 |
|
| 4619 |
// Negative round ups to 0 produce -0 |
4619 |
// Negative round ups to 0 produce -0 |
| 4620 |
if (KnownSrc.isKnownNever(fcPosFinite)) |
4620 |
if (KnownSrc.isKnownNever(fcPosFinite)) |
| 4621 |
Known.knownNot(fcPosFinite); |
4621 |
Known.knownNot(fcPosFinite); |
| 4622 |
if (KnownSrc.isKnownNever(fcNegFinite)) |
4622 |
if (KnownSrc.isKnownNever(fcNegFinite)) |
| 4623 |
Known.knownNot(fcNegFinite); |
4623 |
Known.knownNot(fcNegFinite); |
| 4624 |
|
4624 |
|
| 4625 |
break; |
4625 |
break; |
| 4626 |
} |
4626 |
} |
| 4627 |
case Intrinsic::exp: |
4627 |
case Intrinsic::exp: |
| 4628 |
case Intrinsic::exp2: { |
4628 |
case Intrinsic::exp2: { |
| 4629 |
Known.knownNot(fcNegative); |
4629 |
Known.knownNot(fcNegative); |
| 4630 |
if ((InterestedClasses & fcNan) == fcNone) |
4630 |
if ((InterestedClasses & fcNan) == fcNone) |
| 4631 |
break; |
4631 |
break; |
| 4632 |
|
4632 |
|
| 4633 |
KnownFPClass KnownSrc; |
4633 |
KnownFPClass KnownSrc; |
| 4634 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4634 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4635 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
4635 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
| 4636 |
if (KnownSrc.isKnownNeverNaN()) { |
4636 |
if (KnownSrc.isKnownNeverNaN()) { |
| 4637 |
Known.knownNot(fcNan); |
4637 |
Known.knownNot(fcNan); |
| 4638 |
Known.SignBit = false; |
4638 |
Known.SignBit = false; |
| 4639 |
} |
4639 |
} |
| 4640 |
|
4640 |
|
| 4641 |
break; |
4641 |
break; |
| 4642 |
} |
4642 |
} |
| 4643 |
case Intrinsic::fptrunc_round: { |
4643 |
case Intrinsic::fptrunc_round: { |
| 4644 |
computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, |
4644 |
computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, |
| 4645 |
Known, Depth, Q); |
4645 |
Known, Depth, Q); |
| 4646 |
break; |
4646 |
break; |
| 4647 |
} |
4647 |
} |
| 4648 |
case Intrinsic::log: |
4648 |
case Intrinsic::log: |
| 4649 |
case Intrinsic::log10: |
4649 |
case Intrinsic::log10: |
| 4650 |
case Intrinsic::log2: |
4650 |
case Intrinsic::log2: |
| 4651 |
case Intrinsic::experimental_constrained_log: |
4651 |
case Intrinsic::experimental_constrained_log: |
| 4652 |
case Intrinsic::experimental_constrained_log10: |
4652 |
case Intrinsic::experimental_constrained_log10: |
| 4653 |
case Intrinsic::experimental_constrained_log2: { |
4653 |
case Intrinsic::experimental_constrained_log2: { |
| 4654 |
// log(+inf) -> +inf |
4654 |
// log(+inf) -> +inf |
| 4655 |
// log([+-]0.0) -> -inf |
4655 |
// log([+-]0.0) -> -inf |
| 4656 |
// log(-inf) -> nan |
4656 |
// log(-inf) -> nan |
| 4657 |
// log(-x) -> nan |
4657 |
// log(-x) -> nan |
| 4658 |
if ((InterestedClasses & (fcNan | fcInf)) == fcNone) |
4658 |
if ((InterestedClasses & (fcNan | fcInf)) == fcNone) |
| 4659 |
break; |
4659 |
break; |
| 4660 |
|
4660 |
|
| 4661 |
FPClassTest InterestedSrcs = InterestedClasses; |
4661 |
FPClassTest InterestedSrcs = InterestedClasses; |
| 4662 |
if ((InterestedClasses & fcNegInf) != fcNone) |
4662 |
if ((InterestedClasses & fcNegInf) != fcNone) |
| 4663 |
InterestedSrcs |= fcZero | fcSubnormal; |
4663 |
InterestedSrcs |= fcZero | fcSubnormal; |
| 4664 |
if ((InterestedClasses & fcNan) != fcNone) |
4664 |
if ((InterestedClasses & fcNan) != fcNone) |
| 4665 |
InterestedSrcs |= fcNan | (fcNegative & ~fcNan); |
4665 |
InterestedSrcs |= fcNan | (fcNegative & ~fcNan); |
| 4666 |
|
4666 |
|
| 4667 |
KnownFPClass KnownSrc; |
4667 |
KnownFPClass KnownSrc; |
| 4668 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs, |
4668 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs, |
| 4669 |
KnownSrc, Depth + 1, Q); |
4669 |
KnownSrc, Depth + 1, Q); |
| 4670 |
|
4670 |
|
| 4671 |
if (KnownSrc.isKnownNeverPosInfinity()) |
4671 |
if (KnownSrc.isKnownNeverPosInfinity()) |
| 4672 |
Known.knownNot(fcPosInf); |
4672 |
Known.knownNot(fcPosInf); |
| 4673 |
|
4673 |
|
| 4674 |
if (KnownSrc.isKnownNeverNaN() && |
4674 |
if (KnownSrc.isKnownNeverNaN() && |
| 4675 |
KnownSrc.cannotBeOrderedLessThanZero()) |
4675 |
KnownSrc.cannotBeOrderedLessThanZero()) |
| 4676 |
Known.knownNot(fcNan); |
4676 |
Known.knownNot(fcNan); |
| 4677 |
|
4677 |
|
| 4678 |
const Function *F = II->getFunction(); |
4678 |
const Function *F = II->getFunction(); |
| 4679 |
if (F && KnownSrc.isKnownNeverLogicalZero(*F, II->getType())) |
4679 |
if (F && KnownSrc.isKnownNeverLogicalZero(*F, II->getType())) |
| 4680 |
Known.knownNot(fcNegInf); |
4680 |
Known.knownNot(fcNegInf); |
| 4681 |
|
4681 |
|
| 4682 |
break; |
4682 |
break; |
| 4683 |
} |
4683 |
} |
| 4684 |
case Intrinsic::powi: { |
4684 |
case Intrinsic::powi: { |
| 4685 |
if ((InterestedClasses & fcNegative) == fcNone) |
4685 |
if ((InterestedClasses & fcNegative) == fcNone) |
| 4686 |
break; |
4686 |
break; |
| 4687 |
|
4687 |
|
| 4688 |
const Value *Exp = II->getArgOperand(1); |
4688 |
const Value *Exp = II->getArgOperand(1); |
| 4689 |
Type *ExpTy = Exp->getType(); |
4689 |
Type *ExpTy = Exp->getType(); |
| 4690 |
unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth(); |
4690 |
unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth(); |
| 4691 |
KnownBits ExponentKnownBits(BitWidth); |
4691 |
KnownBits ExponentKnownBits(BitWidth); |
| 4692 |
computeKnownBits(Exp, |
4692 |
computeKnownBits(Exp, |
| 4693 |
isa(ExpTy) ? DemandedElts : APInt(1, 1), |
4693 |
isa(ExpTy) ? DemandedElts : APInt(1, 1), |
| 4694 |
ExponentKnownBits, Depth + 1, Q); |
4694 |
ExponentKnownBits, Depth + 1, Q); |
| 4695 |
|
4695 |
|
| 4696 |
if (ExponentKnownBits.Zero[0]) { // Is even |
4696 |
if (ExponentKnownBits.Zero[0]) { // Is even |
| 4697 |
Known.knownNot(fcNegative); |
4697 |
Known.knownNot(fcNegative); |
| 4698 |
break; |
4698 |
break; |
| 4699 |
} |
4699 |
} |
| 4700 |
|
4700 |
|
| 4701 |
// Given that exp is an integer, here are the |
4701 |
// Given that exp is an integer, here are the |
| 4702 |
// ways that pow can return a negative value: |
4702 |
// ways that pow can return a negative value: |
| 4703 |
// |
4703 |
// |
| 4704 |
// pow(-x, exp) --> negative if exp is odd and x is negative. |
4704 |
// pow(-x, exp) --> negative if exp is odd and x is negative. |
| 4705 |
// pow(-0, exp) --> -inf if exp is negative odd. |
4705 |
// pow(-0, exp) --> -inf if exp is negative odd. |
| 4706 |
// pow(-0, exp) --> -0 if exp is positive odd. |
4706 |
// pow(-0, exp) --> -0 if exp is positive odd. |
| 4707 |
// pow(-inf, exp) --> -0 if exp is negative odd. |
4707 |
// pow(-inf, exp) --> -0 if exp is negative odd. |
| 4708 |
// pow(-inf, exp) --> -inf if exp is positive odd. |
4708 |
// pow(-inf, exp) --> -inf if exp is positive odd. |
| 4709 |
KnownFPClass KnownSrc; |
4709 |
KnownFPClass KnownSrc; |
| 4710 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative, |
4710 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative, |
| 4711 |
KnownSrc, Depth + 1, Q); |
4711 |
KnownSrc, Depth + 1, Q); |
| 4712 |
if (KnownSrc.isKnownNever(fcNegative)) |
4712 |
if (KnownSrc.isKnownNever(fcNegative)) |
| 4713 |
Known.knownNot(fcNegative); |
4713 |
Known.knownNot(fcNegative); |
| 4714 |
break; |
4714 |
break; |
| 4715 |
} |
4715 |
} |
| 4716 |
case Intrinsic::ldexp: { |
4716 |
case Intrinsic::ldexp: { |
| 4717 |
KnownFPClass KnownSrc; |
4717 |
KnownFPClass KnownSrc; |
| 4718 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4718 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4719 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
4719 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
| 4720 |
Known.propagateNaN(KnownSrc, /*PropagateSign=*/true); |
4720 |
Known.propagateNaN(KnownSrc, /*PropagateSign=*/true); |
| 4721 |
|
4721 |
|
| 4722 |
// Sign is preserved, but underflows may produce zeroes. |
4722 |
// Sign is preserved, but underflows may produce zeroes. |
| 4723 |
if (KnownSrc.isKnownNever(fcNegative)) |
4723 |
if (KnownSrc.isKnownNever(fcNegative)) |
| 4724 |
Known.knownNot(fcNegative); |
4724 |
Known.knownNot(fcNegative); |
| 4725 |
else if (KnownSrc.cannotBeOrderedLessThanZero()) |
4725 |
else if (KnownSrc.cannotBeOrderedLessThanZero()) |
| 4726 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
4726 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
| 4727 |
|
4727 |
|
| 4728 |
if (KnownSrc.isKnownNever(fcPositive)) |
4728 |
if (KnownSrc.isKnownNever(fcPositive)) |
| 4729 |
Known.knownNot(fcPositive); |
4729 |
Known.knownNot(fcPositive); |
| 4730 |
else if (KnownSrc.cannotBeOrderedGreaterThanZero()) |
4730 |
else if (KnownSrc.cannotBeOrderedGreaterThanZero()) |
| 4731 |
Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask); |
4731 |
Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask); |
| 4732 |
|
4732 |
|
| 4733 |
// Can refine inf/zero handling based on the exponent operand. |
4733 |
// Can refine inf/zero handling based on the exponent operand. |
| 4734 |
const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf; |
4734 |
const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf; |
| 4735 |
if ((InterestedClasses & ExpInfoMask) == fcNone) |
4735 |
if ((InterestedClasses & ExpInfoMask) == fcNone) |
| 4736 |
break; |
4736 |
break; |
| 4737 |
if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone) |
4737 |
if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone) |
| 4738 |
break; |
4738 |
break; |
| 4739 |
|
4739 |
|
| 4740 |
const fltSemantics &Flt |
4740 |
const fltSemantics &Flt |
| 4741 |
= II->getType()->getScalarType()->getFltSemantics(); |
4741 |
= II->getType()->getScalarType()->getFltSemantics(); |
| 4742 |
unsigned Precision = APFloat::semanticsPrecision(Flt); |
4742 |
unsigned Precision = APFloat::semanticsPrecision(Flt); |
| 4743 |
const Value *ExpArg = II->getArgOperand(1); |
4743 |
const Value *ExpArg = II->getArgOperand(1); |
| 4744 |
ConstantRange ExpRange = computeConstantRange( |
4744 |
ConstantRange ExpRange = computeConstantRange( |
| 4745 |
ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1); |
4745 |
ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1); |
| 4746 |
|
4746 |
|
| 4747 |
const int MantissaBits = Precision - 1; |
4747 |
const int MantissaBits = Precision - 1; |
| 4748 |
if (ExpRange.getSignedMin().sge(static_cast(MantissaBits))) |
4748 |
if (ExpRange.getSignedMin().sge(static_cast(MantissaBits))) |
| 4749 |
Known.knownNot(fcSubnormal); |
4749 |
Known.knownNot(fcSubnormal); |
| 4750 |
|
4750 |
|
| 4751 |
const Function *F = II->getFunction(); |
4751 |
const Function *F = II->getFunction(); |
| 4752 |
const APInt *ConstVal = ExpRange.getSingleElement(); |
4752 |
const APInt *ConstVal = ExpRange.getSingleElement(); |
| 4753 |
if (ConstVal && ConstVal->isZero()) { |
4753 |
if (ConstVal && ConstVal->isZero()) { |
| 4754 |
// ldexp(x, 0) -> x, so propagate everything. |
4754 |
// ldexp(x, 0) -> x, so propagate everything. |
| 4755 |
Known.propagateCanonicalizingSrc(KnownSrc, *F, |
4755 |
Known.propagateCanonicalizingSrc(KnownSrc, *F, |
| 4756 |
II->getType()); |
4756 |
II->getType()); |
| 4757 |
} else if (ExpRange.isAllNegative()) { |
4757 |
} else if (ExpRange.isAllNegative()) { |
| 4758 |
// If we know the power is <= 0, can't introduce inf |
4758 |
// If we know the power is <= 0, can't introduce inf |
| 4759 |
if (KnownSrc.isKnownNeverPosInfinity()) |
4759 |
if (KnownSrc.isKnownNeverPosInfinity()) |
| 4760 |
Known.knownNot(fcPosInf); |
4760 |
Known.knownNot(fcPosInf); |
| 4761 |
if (KnownSrc.isKnownNeverNegInfinity()) |
4761 |
if (KnownSrc.isKnownNeverNegInfinity()) |
| 4762 |
Known.knownNot(fcNegInf); |
4762 |
Known.knownNot(fcNegInf); |
| 4763 |
} else if (ExpRange.isAllNonNegative()) { |
4763 |
} else if (ExpRange.isAllNonNegative()) { |
| 4764 |
// If we know the power is >= 0, can't introduce subnormal or zero |
4764 |
// If we know the power is >= 0, can't introduce subnormal or zero |
| 4765 |
if (KnownSrc.isKnownNeverPosSubnormal()) |
4765 |
if (KnownSrc.isKnownNeverPosSubnormal()) |
| 4766 |
Known.knownNot(fcPosSubnormal); |
4766 |
Known.knownNot(fcPosSubnormal); |
| 4767 |
if (KnownSrc.isKnownNeverNegSubnormal()) |
4767 |
if (KnownSrc.isKnownNeverNegSubnormal()) |
| 4768 |
Known.knownNot(fcNegSubnormal); |
4768 |
Known.knownNot(fcNegSubnormal); |
| 4769 |
if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, II->getType())) |
4769 |
if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, II->getType())) |
| 4770 |
Known.knownNot(fcPosZero); |
4770 |
Known.knownNot(fcPosZero); |
| 4771 |
if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType())) |
4771 |
if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType())) |
| 4772 |
Known.knownNot(fcNegZero); |
4772 |
Known.knownNot(fcNegZero); |
| 4773 |
} |
4773 |
} |
| 4774 |
|
4774 |
|
| 4775 |
break; |
4775 |
break; |
| 4776 |
} |
4776 |
} |
| 4777 |
case Intrinsic::arithmetic_fence: { |
4777 |
case Intrinsic::arithmetic_fence: { |
| 4778 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
4778 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 4779 |
InterestedClasses, Known, Depth + 1, Q); |
4779 |
InterestedClasses, Known, Depth + 1, Q); |
| 4780 |
break; |
4780 |
break; |
| 4781 |
} |
4781 |
} |
| 4782 |
case Intrinsic::experimental_constrained_sitofp: |
4782 |
case Intrinsic::experimental_constrained_sitofp: |
| 4783 |
case Intrinsic::experimental_constrained_uitofp: |
4783 |
case Intrinsic::experimental_constrained_uitofp: |
| 4784 |
// Cannot produce nan |
4784 |
// Cannot produce nan |
| 4785 |
Known.knownNot(fcNan); |
4785 |
Known.knownNot(fcNan); |
| 4786 |
|
4786 |
|
| 4787 |
// sitofp and uitofp turn into +0.0 for zero. |
4787 |
// sitofp and uitofp turn into +0.0 for zero. |
| 4788 |
Known.knownNot(fcNegZero); |
4788 |
Known.knownNot(fcNegZero); |
| 4789 |
|
4789 |
|
| 4790 |
// Integers cannot be subnormal |
4790 |
// Integers cannot be subnormal |
| 4791 |
Known.knownNot(fcSubnormal); |
4791 |
Known.knownNot(fcSubnormal); |
| 4792 |
|
4792 |
|
| 4793 |
if (IID == Intrinsic::experimental_constrained_uitofp) |
4793 |
if (IID == Intrinsic::experimental_constrained_uitofp) |
| 4794 |
Known.signBitMustBeZero(); |
4794 |
Known.signBitMustBeZero(); |
| 4795 |
|
4795 |
|
| 4796 |
// TODO: Copy inf handling from instructions |
4796 |
// TODO: Copy inf handling from instructions |
| 4797 |
break; |
4797 |
break; |
| 4798 |
default: |
4798 |
default: |
| 4799 |
break; |
4799 |
break; |
| 4800 |
} |
4800 |
} |
| 4801 |
} |
4801 |
} |
| 4802 |
|
4802 |
|
| 4803 |
break; |
4803 |
break; |
| 4804 |
} |
4804 |
} |
| 4805 |
case Instruction::FAdd: |
4805 |
case Instruction::FAdd: |
| 4806 |
case Instruction::FSub: { |
4806 |
case Instruction::FSub: { |
| 4807 |
KnownFPClass KnownLHS, KnownRHS; |
4807 |
KnownFPClass KnownLHS, KnownRHS; |
| 4808 |
bool WantNegative = |
4808 |
bool WantNegative = |
| 4809 |
Op->getOpcode() == Instruction::FAdd && |
4809 |
Op->getOpcode() == Instruction::FAdd && |
| 4810 |
(InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone; |
4810 |
(InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone; |
| 4811 |
bool WantNaN = (InterestedClasses & fcNan) != fcNone; |
4811 |
bool WantNaN = (InterestedClasses & fcNan) != fcNone; |
| 4812 |
bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone; |
4812 |
bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone; |
| 4813 |
|
4813 |
|
| 4814 |
if (!WantNaN && !WantNegative && !WantNegZero) |
4814 |
if (!WantNaN && !WantNegative && !WantNegZero) |
| 4815 |
break; |
4815 |
break; |
| 4816 |
|
4816 |
|
| 4817 |
FPClassTest InterestedSrcs = InterestedClasses; |
4817 |
FPClassTest InterestedSrcs = InterestedClasses; |
| 4818 |
if (WantNegative) |
4818 |
if (WantNegative) |
| 4819 |
InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask; |
4819 |
InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask; |
| 4820 |
if (InterestedClasses & fcNan) |
4820 |
if (InterestedClasses & fcNan) |
| 4821 |
InterestedSrcs |= fcInf; |
4821 |
InterestedSrcs |= fcInf; |
| 4822 |
computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs, |
4822 |
computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs, |
| 4823 |
KnownRHS, Depth + 1, Q); |
4823 |
KnownRHS, Depth + 1, Q); |
| 4824 |
|
4824 |
|
| 4825 |
if ((WantNaN && KnownRHS.isKnownNeverNaN()) || |
4825 |
if ((WantNaN && KnownRHS.isKnownNeverNaN()) || |
| 4826 |
(WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) || |
4826 |
(WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) || |
| 4827 |
WantNegZero || Opc == Instruction::FSub) { |
4827 |
WantNegZero || Opc == Instruction::FSub) { |
| 4828 |
|
4828 |
|
| 4829 |
// RHS is canonically cheaper to compute. Skip inspecting the LHS if |
4829 |
// RHS is canonically cheaper to compute. Skip inspecting the LHS if |
| 4830 |
// there's no point. |
4830 |
// there's no point. |
| 4831 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs, |
4831 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs, |
| 4832 |
KnownLHS, Depth + 1, Q); |
4832 |
KnownLHS, Depth + 1, Q); |
| 4833 |
// Adding positive and negative infinity produces NaN. |
4833 |
// Adding positive and negative infinity produces NaN. |
| 4834 |
// TODO: Check sign of infinities. |
4834 |
// TODO: Check sign of infinities. |
| 4835 |
if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
4835 |
if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
| 4836 |
(KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity())) |
4836 |
(KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity())) |
| 4837 |
Known.knownNot(fcNan); |
4837 |
Known.knownNot(fcNan); |
| 4838 |
|
4838 |
|
| 4839 |
// FIXME: Context function should always be passed in separately |
4839 |
// FIXME: Context function should always be passed in separately |
| 4840 |
const Function *F = cast(Op)->getFunction(); |
4840 |
const Function *F = cast(Op)->getFunction(); |
| 4841 |
|
4841 |
|
| 4842 |
if (Op->getOpcode() == Instruction::FAdd) { |
4842 |
if (Op->getOpcode() == Instruction::FAdd) { |
| 4843 |
if (KnownLHS.cannotBeOrderedLessThanZero() && |
4843 |
if (KnownLHS.cannotBeOrderedLessThanZero() && |
| 4844 |
KnownRHS.cannotBeOrderedLessThanZero()) |
4844 |
KnownRHS.cannotBeOrderedLessThanZero()) |
| 4845 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
4845 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
| 4846 |
if (!F) |
4846 |
if (!F) |
| 4847 |
break; |
4847 |
break; |
| 4848 |
|
4848 |
|
| 4849 |
// (fadd x, 0.0) is guaranteed to return +0.0, not -0.0. |
4849 |
// (fadd x, 0.0) is guaranteed to return +0.0, not -0.0. |
| 4850 |
if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) || |
4850 |
if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) || |
| 4851 |
KnownRHS.isKnownNeverLogicalNegZero(*F, Op->getType())) && |
4851 |
KnownRHS.isKnownNeverLogicalNegZero(*F, Op->getType())) && |
| 4852 |
// Make sure output negative denormal can't flush to -0 |
4852 |
// Make sure output negative denormal can't flush to -0 |
| 4853 |
outputDenormalIsIEEEOrPosZero(*F, Op->getType())) |
4853 |
outputDenormalIsIEEEOrPosZero(*F, Op->getType())) |
| 4854 |
Known.knownNot(fcNegZero); |
4854 |
Known.knownNot(fcNegZero); |
| 4855 |
} else { |
4855 |
} else { |
| 4856 |
if (!F) |
4856 |
if (!F) |
| 4857 |
break; |
4857 |
break; |
| 4858 |
|
4858 |
|
| 4859 |
// Only fsub -0, +0 can return -0 |
4859 |
// Only fsub -0, +0 can return -0 |
| 4860 |
if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) || |
4860 |
if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) || |
| 4861 |
KnownRHS.isKnownNeverLogicalPosZero(*F, Op->getType())) && |
4861 |
KnownRHS.isKnownNeverLogicalPosZero(*F, Op->getType())) && |
| 4862 |
// Make sure output negative denormal can't flush to -0 |
4862 |
// Make sure output negative denormal can't flush to -0 |
| 4863 |
outputDenormalIsIEEEOrPosZero(*F, Op->getType())) |
4863 |
outputDenormalIsIEEEOrPosZero(*F, Op->getType())) |
| 4864 |
Known.knownNot(fcNegZero); |
4864 |
Known.knownNot(fcNegZero); |
| 4865 |
} |
4865 |
} |
| 4866 |
} |
4866 |
} |
| 4867 |
|
4867 |
|
| 4868 |
break; |
4868 |
break; |
| 4869 |
} |
4869 |
} |
| 4870 |
case Instruction::FMul: { |
4870 |
case Instruction::FMul: { |
| 4871 |
// X * X is always non-negative or a NaN. |
4871 |
// X * X is always non-negative or a NaN. |
| 4872 |
if (Op->getOperand(0) == Op->getOperand(1)) |
4872 |
if (Op->getOperand(0) == Op->getOperand(1)) |
| 4873 |
Known.knownNot(fcNegative); |
4873 |
Known.knownNot(fcNegative); |
| 4874 |
|
4874 |
|
| 4875 |
if ((InterestedClasses & fcNan) != fcNan) |
4875 |
if ((InterestedClasses & fcNan) != fcNan) |
| 4876 |
break; |
4876 |
break; |
| 4877 |
|
4877 |
|
| 4878 |
// fcSubnormal is only needed in case of DAZ. |
4878 |
// fcSubnormal is only needed in case of DAZ. |
| 4879 |
const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal; |
4879 |
const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal; |
| 4880 |
|
4880 |
|
| 4881 |
KnownFPClass KnownLHS, KnownRHS; |
4881 |
KnownFPClass KnownLHS, KnownRHS; |
| 4882 |
computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS, |
4882 |
computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS, |
| 4883 |
Depth + 1, Q); |
4883 |
Depth + 1, Q); |
| 4884 |
if (!KnownRHS.isKnownNeverNaN()) |
4884 |
if (!KnownRHS.isKnownNeverNaN()) |
| 4885 |
break; |
4885 |
break; |
| 4886 |
|
4886 |
|
| 4887 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS, |
4887 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS, |
| 4888 |
Depth + 1, Q); |
4888 |
Depth + 1, Q); |
| 4889 |
if (!KnownLHS.isKnownNeverNaN()) |
4889 |
if (!KnownLHS.isKnownNeverNaN()) |
| 4890 |
break; |
4890 |
break; |
| 4891 |
|
4891 |
|
| 4892 |
// If 0 * +/-inf produces NaN. |
4892 |
// If 0 * +/-inf produces NaN. |
| 4893 |
if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) { |
4893 |
if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) { |
| 4894 |
Known.knownNot(fcNan); |
4894 |
Known.knownNot(fcNan); |
| 4895 |
break; |
4895 |
break; |
| 4896 |
} |
4896 |
} |
| 4897 |
|
4897 |
|
| 4898 |
const Function *F = cast(Op)->getFunction(); |
4898 |
const Function *F = cast(Op)->getFunction(); |
| 4899 |
if (!F) |
4899 |
if (!F) |
| 4900 |
break; |
4900 |
break; |
| 4901 |
|
4901 |
|
| 4902 |
if ((KnownRHS.isKnownNeverInfinity() || |
4902 |
if ((KnownRHS.isKnownNeverInfinity() || |
| 4903 |
KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) && |
4903 |
KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) && |
| 4904 |
(KnownLHS.isKnownNeverInfinity() || |
4904 |
(KnownLHS.isKnownNeverInfinity() || |
| 4905 |
KnownRHS.isKnownNeverLogicalZero(*F, Op->getType()))) |
4905 |
KnownRHS.isKnownNeverLogicalZero(*F, Op->getType()))) |
| 4906 |
Known.knownNot(fcNan); |
4906 |
Known.knownNot(fcNan); |
| 4907 |
|
4907 |
|
| 4908 |
break; |
4908 |
break; |
| 4909 |
} |
4909 |
} |
| 4910 |
case Instruction::FDiv: |
4910 |
case Instruction::FDiv: |
| 4911 |
case Instruction::FRem: { |
4911 |
case Instruction::FRem: { |
| 4912 |
if (Op->getOperand(0) == Op->getOperand(1)) { |
4912 |
if (Op->getOperand(0) == Op->getOperand(1)) { |
| 4913 |
// TODO: Could filter out snan if we inspect the operand |
4913 |
// TODO: Could filter out snan if we inspect the operand |
| 4914 |
if (Op->getOpcode() == Instruction::FDiv) { |
4914 |
if (Op->getOpcode() == Instruction::FDiv) { |
| 4915 |
// X / X is always exactly 1.0 or a NaN. |
4915 |
// X / X is always exactly 1.0 or a NaN. |
| 4916 |
Known.KnownFPClasses = fcNan | fcPosNormal; |
4916 |
Known.KnownFPClasses = fcNan | fcPosNormal; |
| 4917 |
} else { |
4917 |
} else { |
| 4918 |
// X % X is always exactly [+-]0.0 or a NaN. |
4918 |
// X % X is always exactly [+-]0.0 or a NaN. |
| 4919 |
Known.KnownFPClasses = fcNan | fcZero; |
4919 |
Known.KnownFPClasses = fcNan | fcZero; |
| 4920 |
} |
4920 |
} |
| 4921 |
|
4921 |
|
| 4922 |
break; |
4922 |
break; |
| 4923 |
} |
4923 |
} |
| 4924 |
|
4924 |
|
| 4925 |
const bool WantNan = (InterestedClasses & fcNan) != fcNone; |
4925 |
const bool WantNan = (InterestedClasses & fcNan) != fcNone; |
| 4926 |
const bool WantNegative = (InterestedClasses & fcNegative) != fcNone; |
4926 |
const bool WantNegative = (InterestedClasses & fcNegative) != fcNone; |
| 4927 |
const bool WantPositive = |
4927 |
const bool WantPositive = |
| 4928 |
Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone; |
4928 |
Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone; |
| 4929 |
if (!WantNan && !WantNegative && !WantPositive) |
4929 |
if (!WantNan && !WantNegative && !WantPositive) |
| 4930 |
break; |
4930 |
break; |
| 4931 |
|
4931 |
|
| 4932 |
KnownFPClass KnownLHS, KnownRHS; |
4932 |
KnownFPClass KnownLHS, KnownRHS; |
| 4933 |
|
4933 |
|
| 4934 |
computeKnownFPClass(Op->getOperand(1), DemandedElts, |
4934 |
computeKnownFPClass(Op->getOperand(1), DemandedElts, |
| 4935 |
fcNan | fcInf | fcZero | fcNegative, KnownRHS, |
4935 |
fcNan | fcInf | fcZero | fcNegative, KnownRHS, |
| 4936 |
Depth + 1, Q); |
4936 |
Depth + 1, Q); |
| 4937 |
|
4937 |
|
| 4938 |
bool KnowSomethingUseful = |
4938 |
bool KnowSomethingUseful = |
| 4939 |
KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative); |
4939 |
KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative); |
| 4940 |
|
4940 |
|
| 4941 |
if (KnowSomethingUseful || WantPositive) { |
4941 |
if (KnowSomethingUseful || WantPositive) { |
| 4942 |
const FPClassTest InterestedLHS = |
4942 |
const FPClassTest InterestedLHS = |
| 4943 |
WantPositive ? fcAllFlags |
4943 |
WantPositive ? fcAllFlags |
| 4944 |
: fcNan | fcInf | fcZero | fcSubnormal | fcNegative; |
4944 |
: fcNan | fcInf | fcZero | fcSubnormal | fcNegative; |
| 4945 |
|
4945 |
|
| 4946 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, |
4946 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, |
| 4947 |
InterestedClasses & InterestedLHS, KnownLHS, |
4947 |
InterestedClasses & InterestedLHS, KnownLHS, |
| 4948 |
Depth + 1, Q); |
4948 |
Depth + 1, Q); |
| 4949 |
} |
4949 |
} |
| 4950 |
|
4950 |
|
| 4951 |
const Function *F = cast(Op)->getFunction(); |
4951 |
const Function *F = cast(Op)->getFunction(); |
| 4952 |
|
4952 |
|
| 4953 |
if (Op->getOpcode() == Instruction::FDiv) { |
4953 |
if (Op->getOpcode() == Instruction::FDiv) { |
| 4954 |
// Only 0/0, Inf/Inf produce NaN. |
4954 |
// Only 0/0, Inf/Inf produce NaN. |
| 4955 |
if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
4955 |
if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
| 4956 |
(KnownLHS.isKnownNeverInfinity() || |
4956 |
(KnownLHS.isKnownNeverInfinity() || |
| 4957 |
KnownRHS.isKnownNeverInfinity()) && |
4957 |
KnownRHS.isKnownNeverInfinity()) && |
| 4958 |
((F && KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) || |
4958 |
((F && KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) || |
| 4959 |
(F && KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))) { |
4959 |
(F && KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))) { |
| 4960 |
Known.knownNot(fcNan); |
4960 |
Known.knownNot(fcNan); |
| 4961 |
} |
4961 |
} |
| 4962 |
|
4962 |
|
| 4963 |
// X / -0.0 is -Inf (or NaN). |
4963 |
// X / -0.0 is -Inf (or NaN). |
| 4964 |
// +X / +X is +X |
4964 |
// +X / +X is +X |
| 4965 |
if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative)) |
4965 |
if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative)) |
| 4966 |
Known.knownNot(fcNegative); |
4966 |
Known.knownNot(fcNegative); |
| 4967 |
} else { |
4967 |
} else { |
| 4968 |
// Inf REM x and x REM 0 produce NaN. |
4968 |
// Inf REM x and x REM 0 produce NaN. |
| 4969 |
if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
4969 |
if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
| 4970 |
KnownLHS.isKnownNeverInfinity() && F && |
4970 |
KnownLHS.isKnownNeverInfinity() && F && |
| 4971 |
KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())) { |
4971 |
KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())) { |
| 4972 |
Known.knownNot(fcNan); |
4972 |
Known.knownNot(fcNan); |
| 4973 |
} |
4973 |
} |
| 4974 |
|
4974 |
|
| 4975 |
// The sign for frem is the same as the first operand. |
4975 |
// The sign for frem is the same as the first operand. |
| 4976 |
if (KnownLHS.cannotBeOrderedLessThanZero()) |
4976 |
if (KnownLHS.cannotBeOrderedLessThanZero()) |
| 4977 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
4977 |
Known.knownNot(KnownFPClass::OrderedLessThanZeroMask); |
| 4978 |
if (KnownLHS.cannotBeOrderedGreaterThanZero()) |
4978 |
if (KnownLHS.cannotBeOrderedGreaterThanZero()) |
| 4979 |
Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask); |
4979 |
Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask); |
| 4980 |
|
4980 |
|
| 4981 |
// See if we can be more aggressive about the sign of 0. |
4981 |
// See if we can be more aggressive about the sign of 0. |
| 4982 |
if (KnownLHS.isKnownNever(fcNegative)) |
4982 |
if (KnownLHS.isKnownNever(fcNegative)) |
| 4983 |
Known.knownNot(fcNegative); |
4983 |
Known.knownNot(fcNegative); |
| 4984 |
if (KnownLHS.isKnownNever(fcPositive)) |
4984 |
if (KnownLHS.isKnownNever(fcPositive)) |
| 4985 |
Known.knownNot(fcPositive); |
4985 |
Known.knownNot(fcPositive); |
| 4986 |
} |
4986 |
} |
| 4987 |
|
4987 |
|
| 4988 |
break; |
4988 |
break; |
| 4989 |
} |
4989 |
} |
| 4990 |
case Instruction::FPExt: { |
4990 |
case Instruction::FPExt: { |
| 4991 |
// Infinity, nan and zero propagate from source. |
4991 |
// Infinity, nan and zero propagate from source. |
| 4992 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses, |
4992 |
computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses, |
| 4993 |
Known, Depth + 1, Q); |
4993 |
Known, Depth + 1, Q); |
| 4994 |
|
4994 |
|
| 4995 |
const fltSemantics &DstTy = |
4995 |
const fltSemantics &DstTy = |
| 4996 |
Op->getType()->getScalarType()->getFltSemantics(); |
4996 |
Op->getType()->getScalarType()->getFltSemantics(); |
| 4997 |
const fltSemantics &SrcTy = |
4997 |
const fltSemantics &SrcTy = |
| 4998 |
Op->getOperand(0)->getType()->getScalarType()->getFltSemantics(); |
4998 |
Op->getOperand(0)->getType()->getScalarType()->getFltSemantics(); |
| 4999 |
|
4999 |
|
| 5000 |
// All subnormal inputs should be in the normal range in the result type. |
5000 |
// All subnormal inputs should be in the normal range in the result type. |
| 5001 |
if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) |
5001 |
if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) |
| 5002 |
Known.knownNot(fcSubnormal); |
5002 |
Known.knownNot(fcSubnormal); |
| 5003 |
|
5003 |
|
| 5004 |
// Sign bit of a nan isn't guaranteed. |
5004 |
// Sign bit of a nan isn't guaranteed. |
| 5005 |
if (!Known.isKnownNeverNaN()) |
5005 |
if (!Known.isKnownNeverNaN()) |
| 5006 |
Known.SignBit = std::nullopt; |
5006 |
Known.SignBit = std::nullopt; |
| 5007 |
break; |
5007 |
break; |
| 5008 |
} |
5008 |
} |
| 5009 |
case Instruction::FPTrunc: { |
5009 |
case Instruction::FPTrunc: { |
| 5010 |
computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, |
5010 |
computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, |
| 5011 |
Depth, Q); |
5011 |
Depth, Q); |
| 5012 |
break; |
5012 |
break; |
| 5013 |
} |
5013 |
} |
| 5014 |
case Instruction::SIToFP: |
5014 |
case Instruction::SIToFP: |
| 5015 |
case Instruction::UIToFP: { |
5015 |
case Instruction::UIToFP: { |
| 5016 |
// Cannot produce nan |
5016 |
// Cannot produce nan |
| 5017 |
Known.knownNot(fcNan); |
5017 |
Known.knownNot(fcNan); |
| 5018 |
|
5018 |
|
| 5019 |
// Integers cannot be subnormal |
5019 |
// Integers cannot be subnormal |
| 5020 |
Known.knownNot(fcSubnormal); |
5020 |
Known.knownNot(fcSubnormal); |
| 5021 |
|
5021 |
|
| 5022 |
// sitofp and uitofp turn into +0.0 for zero. |
5022 |
// sitofp and uitofp turn into +0.0 for zero. |
| 5023 |
Known.knownNot(fcNegZero); |
5023 |
Known.knownNot(fcNegZero); |
| 5024 |
if (Op->getOpcode() == Instruction::UIToFP) |
5024 |
if (Op->getOpcode() == Instruction::UIToFP) |
| 5025 |
Known.signBitMustBeZero(); |
5025 |
Known.signBitMustBeZero(); |
| 5026 |
|
5026 |
|
| 5027 |
if (InterestedClasses & fcInf) { |
5027 |
if (InterestedClasses & fcInf) { |
| 5028 |
// Get width of largest magnitude integer (remove a bit if signed). |
5028 |
// Get width of largest magnitude integer (remove a bit if signed). |
| 5029 |
// This still works for a signed minimum value because the largest FP |
5029 |
// This still works for a signed minimum value because the largest FP |
| 5030 |
// value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx). |
5030 |
// value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx). |
| 5031 |
int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits(); |
5031 |
int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits(); |
| 5032 |
if (Op->getOpcode() == Instruction::SIToFP) |
5032 |
if (Op->getOpcode() == Instruction::SIToFP) |
| 5033 |
--IntSize; |
5033 |
--IntSize; |
| 5034 |
|
5034 |
|
| 5035 |
// If the exponent of the largest finite FP value can hold the largest |
5035 |
// If the exponent of the largest finite FP value can hold the largest |
| 5036 |
// integer, the result of the cast must be finite. |
5036 |
// integer, the result of the cast must be finite. |
| 5037 |
Type *FPTy = Op->getType()->getScalarType(); |
5037 |
Type *FPTy = Op->getType()->getScalarType(); |
| 5038 |
if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize) |
5038 |
if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize) |
| 5039 |
Known.knownNot(fcInf); |
5039 |
Known.knownNot(fcInf); |
| 5040 |
} |
5040 |
} |
| 5041 |
|
5041 |
|
| 5042 |
break; |
5042 |
break; |
| 5043 |
} |
5043 |
} |
| 5044 |
case Instruction::ExtractElement: { |
5044 |
case Instruction::ExtractElement: { |
| 5045 |
// Look through extract element. If the index is non-constant or |
5045 |
// Look through extract element. If the index is non-constant or |
| 5046 |
// out-of-range demand all elements, otherwise just the extracted element. |
5046 |
// out-of-range demand all elements, otherwise just the extracted element. |
| 5047 |
const Value *Vec = Op->getOperand(0); |
5047 |
const Value *Vec = Op->getOperand(0); |
| 5048 |
const Value *Idx = Op->getOperand(1); |
5048 |
const Value *Idx = Op->getOperand(1); |
| 5049 |
auto *CIdx = dyn_cast(Idx); |
5049 |
auto *CIdx = dyn_cast(Idx); |
| 5050 |
|
5050 |
|
| 5051 |
if (auto *VecTy = dyn_cast(Vec->getType())) { |
5051 |
if (auto *VecTy = dyn_cast(Vec->getType())) { |
| 5052 |
unsigned NumElts = VecTy->getNumElements(); |
5052 |
unsigned NumElts = VecTy->getNumElements(); |
| 5053 |
APInt DemandedVecElts = APInt::getAllOnes(NumElts); |
5053 |
APInt DemandedVecElts = APInt::getAllOnes(NumElts); |
| 5054 |
if (CIdx && CIdx->getValue().ult(NumElts)) |
5054 |
if (CIdx && CIdx->getValue().ult(NumElts)) |
| 5055 |
DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue()); |
5055 |
DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue()); |
| 5056 |
return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known, |
5056 |
return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known, |
| 5057 |
Depth + 1, Q); |
5057 |
Depth + 1, Q); |
| 5058 |
} |
5058 |
} |
| 5059 |
|
5059 |
|
| 5060 |
break; |
5060 |
break; |
| 5061 |
} |
5061 |
} |
| 5062 |
case Instruction::InsertElement: { |
5062 |
case Instruction::InsertElement: { |
| 5063 |
if (isa(Op->getType())) |
5063 |
if (isa(Op->getType())) |
| 5064 |
return; |
5064 |
return; |
| 5065 |
|
5065 |
|
| 5066 |
const Value *Vec = Op->getOperand(0); |
5066 |
const Value *Vec = Op->getOperand(0); |
| 5067 |
const Value *Elt = Op->getOperand(1); |
5067 |
const Value *Elt = Op->getOperand(1); |
| 5068 |
auto *CIdx = dyn_cast(Op->getOperand(2)); |
5068 |
auto *CIdx = dyn_cast(Op->getOperand(2)); |
| 5069 |
// Early out if the index is non-constant or out-of-range. |
5069 |
// Early out if the index is non-constant or out-of-range. |
| 5070 |
unsigned NumElts = DemandedElts.getBitWidth(); |
5070 |
unsigned NumElts = DemandedElts.getBitWidth(); |
| 5071 |
if (!CIdx || CIdx->getValue().uge(NumElts)) |
5071 |
if (!CIdx || CIdx->getValue().uge(NumElts)) |
| 5072 |
return; |
5072 |
return; |
| 5073 |
|
5073 |
|
| 5074 |
unsigned EltIdx = CIdx->getZExtValue(); |
5074 |
unsigned EltIdx = CIdx->getZExtValue(); |
| 5075 |
// Do we demand the inserted element? |
5075 |
// Do we demand the inserted element? |
| 5076 |
if (DemandedElts[EltIdx]) { |
5076 |
if (DemandedElts[EltIdx]) { |
| 5077 |
computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1, Q); |
5077 |
computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1, Q); |
| 5078 |
// If we don't know any bits, early out. |
5078 |
// If we don't know any bits, early out. |
| 5079 |
if (Known.isUnknown()) |
5079 |
if (Known.isUnknown()) |
| 5080 |
break; |
5080 |
break; |
| 5081 |
} else { |
5081 |
} else { |
| 5082 |
Known.KnownFPClasses = fcNone; |
5082 |
Known.KnownFPClasses = fcNone; |
| 5083 |
} |
5083 |
} |
| 5084 |
|
5084 |
|
| 5085 |
// We don't need the base vector element that has been inserted. |
5085 |
// We don't need the base vector element that has been inserted. |
| 5086 |
APInt DemandedVecElts = DemandedElts; |
5086 |
APInt DemandedVecElts = DemandedElts; |
| 5087 |
DemandedVecElts.clearBit(EltIdx); |
5087 |
DemandedVecElts.clearBit(EltIdx); |
| 5088 |
if (!!DemandedVecElts) { |
5088 |
if (!!DemandedVecElts) { |
| 5089 |
KnownFPClass Known2; |
5089 |
KnownFPClass Known2; |
| 5090 |
computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, |
5090 |
computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, |
| 5091 |
Depth + 1, Q); |
5091 |
Depth + 1, Q); |
| 5092 |
Known |= Known2; |
5092 |
Known |= Known2; |
| 5093 |
} |
5093 |
} |
| 5094 |
|
5094 |
|
| 5095 |
break; |
5095 |
break; |
| 5096 |
} |
5096 |
} |
| 5097 |
case Instruction::ShuffleVector: { |
5097 |
case Instruction::ShuffleVector: { |
| 5098 |
// For undef elements, we don't know anything about the common state of |
5098 |
// For undef elements, we don't know anything about the common state of |
| 5099 |
// the shuffle result. |
5099 |
// the shuffle result. |
| 5100 |
APInt DemandedLHS, DemandedRHS; |
5100 |
APInt DemandedLHS, DemandedRHS; |
| 5101 |
auto *Shuf = dyn_cast(Op); |
5101 |
auto *Shuf = dyn_cast(Op); |
| 5102 |
if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) |
5102 |
if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) |
| 5103 |
return; |
5103 |
return; |
| 5104 |
|
5104 |
|
| 5105 |
if (!!DemandedLHS) { |
5105 |
if (!!DemandedLHS) { |
| 5106 |
const Value *LHS = Shuf->getOperand(0); |
5106 |
const Value *LHS = Shuf->getOperand(0); |
| 5107 |
computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, |
5107 |
computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, |
| 5108 |
Depth + 1, Q); |
5108 |
Depth + 1, Q); |
| 5109 |
|
5109 |
|
| 5110 |
// If we don't know any bits, early out. |
5110 |
// If we don't know any bits, early out. |
| 5111 |
if (Known.isUnknown()) |
5111 |
if (Known.isUnknown()) |
| 5112 |
break; |
5112 |
break; |
| 5113 |
} else { |
5113 |
} else { |
| 5114 |
Known.KnownFPClasses = fcNone; |
5114 |
Known.KnownFPClasses = fcNone; |
| 5115 |
} |
5115 |
} |
| 5116 |
|
5116 |
|
| 5117 |
if (!!DemandedRHS) { |
5117 |
if (!!DemandedRHS) { |
| 5118 |
KnownFPClass Known2; |
5118 |
KnownFPClass Known2; |
| 5119 |
const Value *RHS = Shuf->getOperand(1); |
5119 |
const Value *RHS = Shuf->getOperand(1); |
| 5120 |
computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, |
5120 |
computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, |
| 5121 |
Depth + 1, Q); |
5121 |
Depth + 1, Q); |
| 5122 |
Known |= Known2; |
5122 |
Known |= Known2; |
| 5123 |
} |
5123 |
} |
| 5124 |
|
5124 |
|
| 5125 |
break; |
5125 |
break; |
| 5126 |
} |
5126 |
} |
| 5127 |
case Instruction::ExtractValue: { |
5127 |
case Instruction::ExtractValue: { |
| 5128 |
const ExtractValueInst *Extract = cast(Op); |
5128 |
const ExtractValueInst *Extract = cast(Op); |
| 5129 |
ArrayRef Indices = Extract->getIndices(); |
5129 |
ArrayRef Indices = Extract->getIndices(); |
| 5130 |
const Value *Src = Extract->getAggregateOperand(); |
5130 |
const Value *Src = Extract->getAggregateOperand(); |
| 5131 |
if (isa(Src->getType()) && Indices.size() == 1 && |
5131 |
if (isa(Src->getType()) && Indices.size() == 1 && |
| 5132 |
Indices[0] == 0) { |
5132 |
Indices[0] == 0) { |
| 5133 |
if (const auto *II = dyn_cast(Src)) { |
5133 |
if (const auto *II = dyn_cast(Src)) { |
| 5134 |
switch (II->getIntrinsicID()) { |
5134 |
switch (II->getIntrinsicID()) { |
| 5135 |
case Intrinsic::frexp: { |
5135 |
case Intrinsic::frexp: { |
| 5136 |
Known.knownNot(fcSubnormal); |
5136 |
Known.knownNot(fcSubnormal); |
| 5137 |
|
5137 |
|
| 5138 |
KnownFPClass KnownSrc; |
5138 |
KnownFPClass KnownSrc; |
| 5139 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
5139 |
computeKnownFPClass(II->getArgOperand(0), DemandedElts, |
| 5140 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
5140 |
InterestedClasses, KnownSrc, Depth + 1, Q); |
| 5141 |
|
5141 |
|
| 5142 |
const Function *F = cast(Op)->getFunction(); |
5142 |
const Function *F = cast(Op)->getFunction(); |
| 5143 |
|
5143 |
|
| 5144 |
if (KnownSrc.isKnownNever(fcNegative)) |
5144 |
if (KnownSrc.isKnownNever(fcNegative)) |
| 5145 |
Known.knownNot(fcNegative); |
5145 |
Known.knownNot(fcNegative); |
| 5146 |
else { |
5146 |
else { |
| 5147 |
if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, Op->getType())) |
5147 |
if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, Op->getType())) |
| 5148 |
Known.knownNot(fcNegZero); |
5148 |
Known.knownNot(fcNegZero); |
| 5149 |
if (KnownSrc.isKnownNever(fcNegInf)) |
5149 |
if (KnownSrc.isKnownNever(fcNegInf)) |
| 5150 |
Known.knownNot(fcNegInf); |
5150 |
Known.knownNot(fcNegInf); |
| 5151 |
} |
5151 |
} |
| 5152 |
|
5152 |
|
| 5153 |
if (KnownSrc.isKnownNever(fcPositive)) |
5153 |
if (KnownSrc.isKnownNever(fcPositive)) |
| 5154 |
Known.knownNot(fcPositive); |
5154 |
Known.knownNot(fcPositive); |
| 5155 |
else { |
5155 |
else { |
| 5156 |
if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, Op->getType())) |
5156 |
if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, Op->getType())) |
| 5157 |
Known.knownNot(fcPosZero); |
5157 |
Known.knownNot(fcPosZero); |
| 5158 |
if (KnownSrc.isKnownNever(fcPosInf)) |
5158 |
if (KnownSrc.isKnownNever(fcPosInf)) |
| 5159 |
Known.knownNot(fcPosInf); |
5159 |
Known.knownNot(fcPosInf); |
| 5160 |
} |
5160 |
} |
| 5161 |
|
5161 |
|
| 5162 |
Known.propagateNaN(KnownSrc); |
5162 |
Known.propagateNaN(KnownSrc); |
| 5163 |
return; |
5163 |
return; |
| 5164 |
} |
5164 |
} |
| 5165 |
default: |
5165 |
default: |
| 5166 |
break; |
5166 |
break; |
| 5167 |
} |
5167 |
} |
| 5168 |
} |
5168 |
} |
| 5169 |
} |
5169 |
} |
| 5170 |
|
5170 |
|
| 5171 |
computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1, |
5171 |
computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1, |
| 5172 |
Q); |
5172 |
Q); |
| 5173 |
break; |
5173 |
break; |
| 5174 |
} |
5174 |
} |
| 5175 |
case Instruction::PHI: { |
5175 |
case Instruction::PHI: { |
| 5176 |
const PHINode *P = cast(Op); |
5176 |
const PHINode *P = cast(Op); |
| 5177 |
// Unreachable blocks may have zero-operand PHI nodes. |
5177 |
// Unreachable blocks may have zero-operand PHI nodes. |
| 5178 |
if (P->getNumIncomingValues() == 0) |
5178 |
if (P->getNumIncomingValues() == 0) |
| 5179 |
break; |
5179 |
break; |
| 5180 |
|
5180 |
|
| 5181 |
// Otherwise take the unions of the known bit sets of the operands, |
5181 |
// Otherwise take the unions of the known bit sets of the operands, |
| 5182 |
// taking conservative care to avoid excessive recursion. |
5182 |
// taking conservative care to avoid excessive recursion. |
| 5183 |
const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2; |
5183 |
const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2; |
| 5184 |
|
5184 |
|
| 5185 |
if (Depth < PhiRecursionLimit) { |
5185 |
if (Depth < PhiRecursionLimit) { |
| 5186 |
// Skip if every incoming value references to ourself. |
5186 |
// Skip if every incoming value references to ourself. |
| 5187 |
if (isa_and_nonnull(P->hasConstantValue())) |
5187 |
if (isa_and_nonnull(P->hasConstantValue())) |
| 5188 |
break; |
5188 |
break; |
| 5189 |
|
5189 |
|
| 5190 |
bool First = true; |
5190 |
bool First = true; |
| 5191 |
|
5191 |
|
| 5192 |
for (Value *IncValue : P->incoming_values()) { |
5192 |
for (Value *IncValue : P->incoming_values()) { |
| 5193 |
// Skip direct self references. |
5193 |
// Skip direct self references. |
| 5194 |
if (IncValue == P) |
5194 |
if (IncValue == P) |
| 5195 |
continue; |
5195 |
continue; |
| 5196 |
|
5196 |
|
| 5197 |
KnownFPClass KnownSrc; |
5197 |
KnownFPClass KnownSrc; |
| 5198 |
// Recurse, but cap the recursion to two levels, because we don't want |
5198 |
// Recurse, but cap the recursion to two levels, because we don't want |
| 5199 |
// to waste time spinning around in loops. We need at least depth 2 to |
5199 |
// to waste time spinning around in loops. We need at least depth 2 to |
| 5200 |
// detect known sign bits. |
5200 |
// detect known sign bits. |
| 5201 |
computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc, |
5201 |
computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc, |
| 5202 |
PhiRecursionLimit, Q); |
5202 |
PhiRecursionLimit, Q); |
| 5203 |
|
5203 |
|
| 5204 |
if (First) { |
5204 |
if (First) { |
| 5205 |
Known = KnownSrc; |
5205 |
Known = KnownSrc; |
| 5206 |
First = false; |
5206 |
First = false; |
| 5207 |
} else { |
5207 |
} else { |
| 5208 |
Known |= KnownSrc; |
5208 |
Known |= KnownSrc; |
| 5209 |
} |
5209 |
} |
| 5210 |
|
5210 |
|
| 5211 |
if (Known.KnownFPClasses == fcAllFlags) |
5211 |
if (Known.KnownFPClasses == fcAllFlags) |
| 5212 |
break; |
5212 |
break; |
| 5213 |
} |
5213 |
} |
| 5214 |
} |
5214 |
} |
| 5215 |
|
5215 |
|
| 5216 |
break; |
5216 |
break; |
| 5217 |
} |
5217 |
} |
| 5218 |
default: |
5218 |
default: |
| 5219 |
break; |
5219 |
break; |
| 5220 |
} |
5220 |
} |
| 5221 |
} |
5221 |
} |
| 5222 |
|
5222 |
|
| 5223 |
KnownFPClass llvm::computeKnownFPClass( |
5223 |
KnownFPClass llvm::computeKnownFPClass( |
| 5224 |
const Value *V, const APInt &DemandedElts, const DataLayout &DL, |
5224 |
const Value *V, const APInt &DemandedElts, const DataLayout &DL, |
| 5225 |
FPClassTest InterestedClasses, unsigned Depth, const TargetLibraryInfo *TLI, |
5225 |
FPClassTest InterestedClasses, unsigned Depth, const TargetLibraryInfo *TLI, |
| 5226 |
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
5226 |
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
| 5227 |
bool UseInstrInfo) { |
5227 |
bool UseInstrInfo) { |
| 5228 |
KnownFPClass KnownClasses; |
5228 |
KnownFPClass KnownClasses; |
| 5229 |
::computeKnownFPClass( |
5229 |
::computeKnownFPClass( |
| 5230 |
V, DemandedElts, InterestedClasses, KnownClasses, Depth, |
5230 |
V, DemandedElts, InterestedClasses, KnownClasses, Depth, |
| 5231 |
SimplifyQuery(DL, TLI, DT, AC, safeCxtI(V, CxtI), UseInstrInfo)); |
5231 |
SimplifyQuery(DL, TLI, DT, AC, safeCxtI(V, CxtI), UseInstrInfo)); |
| 5232 |
return KnownClasses; |
5232 |
return KnownClasses; |
| 5233 |
} |
5233 |
} |
| 5234 |
|
5234 |
|
| 5235 |
KnownFPClass llvm::computeKnownFPClass( |
5235 |
KnownFPClass llvm::computeKnownFPClass( |
| 5236 |
const Value *V, const DataLayout &DL, FPClassTest InterestedClasses, |
5236 |
const Value *V, const DataLayout &DL, FPClassTest InterestedClasses, |
| 5237 |
unsigned Depth, const TargetLibraryInfo *TLI, AssumptionCache *AC, |
5237 |
unsigned Depth, const TargetLibraryInfo *TLI, AssumptionCache *AC, |
| 5238 |
const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { |
5238 |
const Instruction *CxtI, const DominatorTree *DT, bool UseInstrInfo) { |
| 5239 |
KnownFPClass Known; |
5239 |
KnownFPClass Known; |
| 5240 |
::computeKnownFPClass( |
5240 |
::computeKnownFPClass( |
| 5241 |
V, Known, InterestedClasses, Depth, |
5241 |
V, Known, InterestedClasses, Depth, |
| 5242 |
SimplifyQuery(DL, TLI, DT, AC, safeCxtI(V, CxtI), UseInstrInfo)); |
5242 |
SimplifyQuery(DL, TLI, DT, AC, safeCxtI(V, CxtI), UseInstrInfo)); |
| 5243 |
return Known; |
5243 |
return Known; |
| 5244 |
} |
5244 |
} |
| 5245 |
|
5245 |
|
| 5246 |
Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { |
5246 |
Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { |
| 5247 |
|
5247 |
|
| 5248 |
// All byte-wide stores are splatable, even of arbitrary variables. |
5248 |
// All byte-wide stores are splatable, even of arbitrary variables. |
| 5249 |
if (V->getType()->isIntegerTy(8)) |
5249 |
if (V->getType()->isIntegerTy(8)) |
| 5250 |
return V; |
5250 |
return V; |
| 5251 |
|
5251 |
|
| 5252 |
LLVMContext &Ctx = V->getContext(); |
5252 |
LLVMContext &Ctx = V->getContext(); |
| 5253 |
|
5253 |
|
| 5254 |
// Undef don't care. |
5254 |
// Undef don't care. |
| 5255 |
auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx)); |
5255 |
auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx)); |
| 5256 |
if (isa(V)) |
5256 |
if (isa(V)) |
| 5257 |
return UndefInt8; |
5257 |
return UndefInt8; |
| 5258 |
|
5258 |
|
| 5259 |
// Return Undef for zero-sized type. |
5259 |
// Return Undef for zero-sized type. |
| 5260 |
if (!DL.getTypeStoreSize(V->getType()).isNonZero()) |
5260 |
if (!DL.getTypeStoreSize(V->getType()).isNonZero()) |
| 5261 |
return UndefInt8; |
5261 |
return UndefInt8; |
| 5262 |
|
5262 |
|
| 5263 |
Constant *C = dyn_cast(V); |
5263 |
Constant *C = dyn_cast(V); |
| 5264 |
if (!C) { |
5264 |
if (!C) { |
| 5265 |
// Conceptually, we could handle things like: |
5265 |
// Conceptually, we could handle things like: |
| 5266 |
// %a = zext i8 %X to i16 |
5266 |
// %a = zext i8 %X to i16 |
| 5267 |
// %b = shl i16 %a, 8 |
5267 |
// %b = shl i16 %a, 8 |
| 5268 |
// %c = or i16 %a, %b |
5268 |
// %c = or i16 %a, %b |
| 5269 |
// but until there is an example that actually needs this, it doesn't seem |
5269 |
// but until there is an example that actually needs this, it doesn't seem |
| 5270 |
// worth worrying about. |
5270 |
// worth worrying about. |
| 5271 |
return nullptr; |
5271 |
return nullptr; |
| 5272 |
} |
5272 |
} |
| 5273 |
|
5273 |
|
| 5274 |
// Handle 'null' ConstantArrayZero etc. |
5274 |
// Handle 'null' ConstantArrayZero etc. |
| 5275 |
if (C->isNullValue()) |
5275 |
if (C->isNullValue()) |
| 5276 |
return Constant::getNullValue(Type::getInt8Ty(Ctx)); |
5276 |
return Constant::getNullValue(Type::getInt8Ty(Ctx)); |
| 5277 |
|
5277 |
|
| 5278 |
// Constant floating-point values can be handled as integer values if the |
5278 |
// Constant floating-point values can be handled as integer values if the |
| 5279 |
// corresponding integer value is "byteable". An important case is 0.0. |
5279 |
// corresponding integer value is "byteable". An important case is 0.0. |
| 5280 |
if (ConstantFP *CFP = dyn_cast(C)) { |
5280 |
if (ConstantFP *CFP = dyn_cast(C)) { |
| 5281 |
Type *Ty = nullptr; |
5281 |
Type *Ty = nullptr; |
| 5282 |
if (CFP->getType()->isHalfTy()) |
5282 |
if (CFP->getType()->isHalfTy()) |
| 5283 |
Ty = Type::getInt16Ty(Ctx); |
5283 |
Ty = Type::getInt16Ty(Ctx); |
| 5284 |
else if (CFP->getType()->isFloatTy()) |
5284 |
else if (CFP->getType()->isFloatTy()) |
| 5285 |
Ty = Type::getInt32Ty(Ctx); |
5285 |
Ty = Type::getInt32Ty(Ctx); |
| 5286 |
else if (CFP->getType()->isDoubleTy()) |
5286 |
else if (CFP->getType()->isDoubleTy()) |
| 5287 |
Ty = Type::getInt64Ty(Ctx); |
5287 |
Ty = Type::getInt64Ty(Ctx); |
| 5288 |
// Don't handle long double formats, which have strange constraints. |
5288 |
// Don't handle long double formats, which have strange constraints. |
| 5289 |
return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL) |
5289 |
return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL) |
| 5290 |
: nullptr; |
5290 |
: nullptr; |
| 5291 |
} |
5291 |
} |
| 5292 |
|
5292 |
|
| 5293 |
// We can handle constant integers that are multiple of 8 bits. |
5293 |
// We can handle constant integers that are multiple of 8 bits. |
| 5294 |
if (ConstantInt *CI = dyn_cast(C)) { |
5294 |
if (ConstantInt *CI = dyn_cast(C)) { |
| 5295 |
if (CI->getBitWidth() % 8 == 0) { |
5295 |
if (CI->getBitWidth() % 8 == 0) { |
| 5296 |
assert(CI->getBitWidth() > 8 && "8 bits should be handled above!"); |
5296 |
assert(CI->getBitWidth() > 8 && "8 bits should be handled above!"); |
| 5297 |
if (!CI->getValue().isSplat(8)) |
5297 |
if (!CI->getValue().isSplat(8)) |
| 5298 |
return nullptr; |
5298 |
return nullptr; |
| 5299 |
return ConstantInt::get(Ctx, CI->getValue().trunc(8)); |
5299 |
return ConstantInt::get(Ctx, CI->getValue().trunc(8)); |
| 5300 |
} |
5300 |
} |
| 5301 |
} |
5301 |
} |
| 5302 |
|
5302 |
|
| 5303 |
if (auto *CE = dyn_cast(C)) { |
5303 |
if (auto *CE = dyn_cast(C)) { |
| 5304 |
if (CE->getOpcode() == Instruction::IntToPtr) { |
5304 |
if (CE->getOpcode() == Instruction::IntToPtr) { |
| 5305 |
if (auto *PtrTy = dyn_cast(CE->getType())) { |
5305 |
if (auto *PtrTy = dyn_cast(CE->getType())) { |
| 5306 |
unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace()); |
5306 |
unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace()); |
| 5307 |
return isBytewiseValue( |
5307 |
return isBytewiseValue( |
| 5308 |
ConstantExpr::getIntegerCast(CE->getOperand(0), |
5308 |
ConstantExpr::getIntegerCast(CE->getOperand(0), |
| 5309 |
Type::getIntNTy(Ctx, BitWidth), false), |
5309 |
Type::getIntNTy(Ctx, BitWidth), false), |
| 5310 |
DL); |
5310 |
DL); |
| 5311 |
} |
5311 |
} |
| 5312 |
} |
5312 |
} |
| 5313 |
} |
5313 |
} |
| 5314 |
|
5314 |
|
| 5315 |
auto Merge = [&](Value *LHS, Value *RHS) -> Value * { |
5315 |
auto Merge = [&](Value *LHS, Value *RHS) -> Value * { |
| 5316 |
if (LHS == RHS) |
5316 |
if (LHS == RHS) |
| 5317 |
return LHS; |
5317 |
return LHS; |
| 5318 |
if (!LHS || !RHS) |
5318 |
if (!LHS || !RHS) |
| 5319 |
return nullptr; |
5319 |
return nullptr; |
| 5320 |
if (LHS == UndefInt8) |
5320 |
if (LHS == UndefInt8) |
| 5321 |
return RHS; |
5321 |
return RHS; |
| 5322 |
if (RHS == UndefInt8) |
5322 |
if (RHS == UndefInt8) |
| 5323 |
return LHS; |
5323 |
return LHS; |
| 5324 |
return nullptr; |
5324 |
return nullptr; |
| 5325 |
}; |
5325 |
}; |
| 5326 |
|
5326 |
|
| 5327 |
if (ConstantDataSequential *CA = dyn_cast(C)) { |
5327 |
if (ConstantDataSequential *CA = dyn_cast(C)) { |
| 5328 |
Value *Val = UndefInt8; |
5328 |
Value *Val = UndefInt8; |
| 5329 |
for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I) |
5329 |
for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I) |
| 5330 |
if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL)))) |
5330 |
if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL)))) |
| 5331 |
return nullptr; |
5331 |
return nullptr; |
| 5332 |
return Val; |
5332 |
return Val; |
| 5333 |
} |
5333 |
} |
| 5334 |
|
5334 |
|
| 5335 |
if (isa(C)) { |
5335 |
if (isa(C)) { |
| 5336 |
Value *Val = UndefInt8; |
5336 |
Value *Val = UndefInt8; |
| 5337 |
for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) |
5337 |
for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) |
| 5338 |
if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL)))) |
5338 |
if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL)))) |
| 5339 |
return nullptr; |
5339 |
return nullptr; |
| 5340 |
return Val; |
5340 |
return Val; |
| 5341 |
} |
5341 |
} |
| 5342 |
|
5342 |
|
| 5343 |
// Don't try to handle the handful of other constants. |
5343 |
// Don't try to handle the handful of other constants. |
| 5344 |
return nullptr; |
5344 |
return nullptr; |
| 5345 |
} |
5345 |
} |
| 5346 |
|
5346 |
|
| 5347 |
// This is the recursive version of BuildSubAggregate. It takes a few different |
5347 |
// This is the recursive version of BuildSubAggregate. It takes a few different |
| 5348 |
// arguments. Idxs is the index within the nested struct From that we are |
5348 |
// arguments. Idxs is the index within the nested struct From that we are |
| 5349 |
// looking at now (which is of type IndexedType). IdxSkip is the number of |
5349 |
// looking at now (which is of type IndexedType). IdxSkip is the number of |
| 5350 |
// indices from Idxs that should be left out when inserting into the resulting |
5350 |
// indices from Idxs that should be left out when inserting into the resulting |
| 5351 |
// struct. To is the result struct built so far, new insertvalue instructions |
5351 |
// struct. To is the result struct built so far, new insertvalue instructions |
| 5352 |
// build on that. |
5352 |
// build on that. |
| 5353 |
static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType, |
5353 |
static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType, |
| 5354 |
SmallVectorImpl &Idxs, |
5354 |
SmallVectorImpl &Idxs, |
| 5355 |
unsigned IdxSkip, |
5355 |
unsigned IdxSkip, |
| 5356 |
Instruction *InsertBefore) { |
5356 |
Instruction *InsertBefore) { |
| 5357 |
StructType *STy = dyn_cast(IndexedType); |
5357 |
StructType *STy = dyn_cast(IndexedType); |
| 5358 |
if (STy) { |
5358 |
if (STy) { |
| 5359 |
// Save the original To argument so we can modify it |
5359 |
// Save the original To argument so we can modify it |
| 5360 |
Value *OrigTo = To; |
5360 |
Value *OrigTo = To; |
| 5361 |
// General case, the type indexed by Idxs is a struct |
5361 |
// General case, the type indexed by Idxs is a struct |
| 5362 |
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
5362 |
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 5363 |
// Process each struct element recursively |
5363 |
// Process each struct element recursively |
| 5364 |
Idxs.push_back(i); |
5364 |
Idxs.push_back(i); |
| 5365 |
Value *PrevTo = To; |
5365 |
Value *PrevTo = To; |
| 5366 |
To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, |
5366 |
To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, |
| 5367 |
InsertBefore); |
5367 |
InsertBefore); |
| 5368 |
Idxs.pop_back(); |
5368 |
Idxs.pop_back(); |
| 5369 |
if (!To) { |
5369 |
if (!To) { |
| 5370 |
// Couldn't find any inserted value for this index? Cleanup |
5370 |
// Couldn't find any inserted value for this index? Cleanup |
| 5371 |
while (PrevTo != OrigTo) { |
5371 |
while (PrevTo != OrigTo) { |
| 5372 |
InsertValueInst* Del = cast(PrevTo); |
5372 |
InsertValueInst* Del = cast(PrevTo); |
| 5373 |
PrevTo = Del->getAggregateOperand(); |
5373 |
PrevTo = Del->getAggregateOperand(); |
| 5374 |
Del->eraseFromParent(); |
5374 |
Del->eraseFromParent(); |
| 5375 |
} |
5375 |
} |
| 5376 |
// Stop processing elements |
5376 |
// Stop processing elements |
| 5377 |
break; |
5377 |
break; |
| 5378 |
} |
5378 |
} |
| 5379 |
} |
5379 |
} |
| 5380 |
// If we successfully found a value for each of our subaggregates |
5380 |
// If we successfully found a value for each of our subaggregates |
| 5381 |
if (To) |
5381 |
if (To) |
| 5382 |
return To; |
5382 |
return To; |
| 5383 |
} |
5383 |
} |
| 5384 |
// Base case, the type indexed by SourceIdxs is not a struct, or not all of |
5384 |
// Base case, the type indexed by SourceIdxs is not a struct, or not all of |
| 5385 |
// the struct's elements had a value that was inserted directly. In the latter |
5385 |
// the struct's elements had a value that was inserted directly. In the latter |
| 5386 |
// case, perhaps we can't determine each of the subelements individually, but |
5386 |
// case, perhaps we can't determine each of the subelements individually, but |
| 5387 |
// we might be able to find the complete struct somewhere. |
5387 |
// we might be able to find the complete struct somewhere. |
| 5388 |
|
5388 |
|
| 5389 |
// Find the value that is at that particular spot |
5389 |
// Find the value that is at that particular spot |
| 5390 |
Value *V = FindInsertedValue(From, Idxs); |
5390 |
Value *V = FindInsertedValue(From, Idxs); |
| 5391 |
|
5391 |
|
| 5392 |
if (!V) |
5392 |
if (!V) |
| 5393 |
return nullptr; |
5393 |
return nullptr; |
| 5394 |
|
5394 |
|
| 5395 |
// Insert the value in the new (sub) aggregate |
5395 |
// Insert the value in the new (sub) aggregate |
| 5396 |
return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp", |
5396 |
return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp", |
| 5397 |
InsertBefore); |
5397 |
InsertBefore); |
| 5398 |
} |
5398 |
} |
| 5399 |
|
5399 |
|
| 5400 |
// This helper takes a nested struct and extracts a part of it (which is again a |
5400 |
// This helper takes a nested struct and extracts a part of it (which is again a |
| 5401 |
// struct) into a new value. For example, given the struct: |
5401 |
// struct) into a new value. For example, given the struct: |
| 5402 |
// { a, { b, { c, d }, e } } |
5402 |
// { a, { b, { c, d }, e } } |
| 5403 |
// and the indices "1, 1" this returns |
5403 |
// and the indices "1, 1" this returns |
| 5404 |
// { c, d }. |
5404 |
// { c, d }. |
| 5405 |
// |
5405 |
// |
| 5406 |
// It does this by inserting an insertvalue for each element in the resulting |
5406 |
// It does this by inserting an insertvalue for each element in the resulting |
| 5407 |
// struct, as opposed to just inserting a single struct. This will only work if |
5407 |
// struct, as opposed to just inserting a single struct. This will only work if |
| 5408 |
// each of the elements of the substruct are known (ie, inserted into From by an |
5408 |
// each of the elements of the substruct are known (ie, inserted into From by an |
| 5409 |
// insertvalue instruction somewhere). |
5409 |
// insertvalue instruction somewhere). |
| 5410 |
// |
5410 |
// |
| 5411 |
// All inserted insertvalue instructions are inserted before InsertBefore |
5411 |
// All inserted insertvalue instructions are inserted before InsertBefore |
| 5412 |
static Value *BuildSubAggregate(Value *From, ArrayRef idx_range, |
5412 |
static Value *BuildSubAggregate(Value *From, ArrayRef idx_range, |
| 5413 |
Instruction *InsertBefore) { |
5413 |
Instruction *InsertBefore) { |
| 5414 |
assert(InsertBefore && "Must have someplace to insert!"); |
5414 |
assert(InsertBefore && "Must have someplace to insert!"); |
| 5415 |
Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), |
5415 |
Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), |
| 5416 |
idx_range); |
5416 |
idx_range); |
| 5417 |
Value *To = PoisonValue::get(IndexedType); |
5417 |
Value *To = PoisonValue::get(IndexedType); |
| 5418 |
SmallVector Idxs(idx_range.begin(), idx_range.end()); |
5418 |
SmallVector Idxs(idx_range.begin(), idx_range.end()); |
| 5419 |
unsigned IdxSkip = Idxs.size(); |
5419 |
unsigned IdxSkip = Idxs.size(); |
| 5420 |
|
5420 |
|
| 5421 |
return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); |
5421 |
return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); |
| 5422 |
} |
5422 |
} |
| 5423 |
|
5423 |
|
| 5424 |
/// Given an aggregate and a sequence of indices, see if the scalar value |
5424 |
/// Given an aggregate and a sequence of indices, see if the scalar value |
| 5425 |
/// indexed is already around as a register, for example if it was inserted |
5425 |
/// indexed is already around as a register, for example if it was inserted |
| 5426 |
/// directly into the aggregate. |
5426 |
/// directly into the aggregate. |
| 5427 |
/// |
5427 |
/// |
| 5428 |
/// If InsertBefore is not null, this function will duplicate (modified) |
5428 |
/// If InsertBefore is not null, this function will duplicate (modified) |
| 5429 |
/// insertvalues when a part of a nested struct is extracted. |
5429 |
/// insertvalues when a part of a nested struct is extracted. |
| 5430 |
Value *llvm::FindInsertedValue(Value *V, ArrayRef idx_range, |
5430 |
Value *llvm::FindInsertedValue(Value *V, ArrayRef idx_range, |
| 5431 |
Instruction *InsertBefore) { |
5431 |
Instruction *InsertBefore) { |
| 5432 |
// Nothing to index? Just return V then (this is useful at the end of our |
5432 |
// Nothing to index? Just return V then (this is useful at the end of our |
| 5433 |
// recursion). |
5433 |
// recursion). |
| 5434 |
if (idx_range.empty()) |
5434 |
if (idx_range.empty()) |
| 5435 |
return V; |
5435 |
return V; |
| 5436 |
// We have indices, so V should have an indexable type. |
5436 |
// We have indices, so V should have an indexable type. |
| 5437 |
assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) && |
5437 |
assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) && |
| 5438 |
"Not looking at a struct or array?"); |
5438 |
"Not looking at a struct or array?"); |
| 5439 |
assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) && |
5439 |
assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) && |
| 5440 |
"Invalid indices for type?"); |
5440 |
"Invalid indices for type?"); |
| 5441 |
|
5441 |
|
| 5442 |
if (Constant *C = dyn_cast(V)) { |
5442 |
if (Constant *C = dyn_cast(V)) { |
| 5443 |
C = C->getAggregateElement(idx_range[0]); |
5443 |
C = C->getAggregateElement(idx_range[0]); |
| 5444 |
if (!C) return nullptr; |
5444 |
if (!C) return nullptr; |
| 5445 |
return FindInsertedValue(C, idx_range.slice(1), InsertBefore); |
5445 |
return FindInsertedValue(C, idx_range.slice(1), InsertBefore); |
| 5446 |
} |
5446 |
} |
| 5447 |
|
5447 |
|
| 5448 |
if (InsertValueInst *I = dyn_cast(V)) { |
5448 |
if (InsertValueInst *I = dyn_cast(V)) { |
| 5449 |
// Loop the indices for the insertvalue instruction in parallel with the |
5449 |
// Loop the indices for the insertvalue instruction in parallel with the |
| 5450 |
// requested indices |
5450 |
// requested indices |
| 5451 |
const unsigned *req_idx = idx_range.begin(); |
5451 |
const unsigned *req_idx = idx_range.begin(); |
| 5452 |
for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); |
5452 |
for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); |
| 5453 |
i != e; ++i, ++req_idx) { |
5453 |
i != e; ++i, ++req_idx) { |
| 5454 |
if (req_idx == idx_range.end()) { |
5454 |
if (req_idx == idx_range.end()) { |
| 5455 |
// We can't handle this without inserting insertvalues |
5455 |
// We can't handle this without inserting insertvalues |
| 5456 |
if (!InsertBefore) |
5456 |
if (!InsertBefore) |
| 5457 |
return nullptr; |
5457 |
return nullptr; |
| 5458 |
|
5458 |
|
| 5459 |
// The requested index identifies a part of a nested aggregate. Handle |
5459 |
// The requested index identifies a part of a nested aggregate. Handle |
| 5460 |
// this specially. For example, |
5460 |
// this specially. For example, |
| 5461 |
// %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 |
5461 |
// %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 |
| 5462 |
// %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 |
5462 |
// %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 |
| 5463 |
// %C = extractvalue {i32, { i32, i32 } } %B, 1 |
5463 |
// %C = extractvalue {i32, { i32, i32 } } %B, 1 |
| 5464 |
// This can be changed into |
5464 |
// This can be changed into |
| 5465 |
// %A = insertvalue {i32, i32 } undef, i32 10, 0 |
5465 |
// %A = insertvalue {i32, i32 } undef, i32 10, 0 |
| 5466 |
// %C = insertvalue {i32, i32 } %A, i32 11, 1 |
5466 |
// %C = insertvalue {i32, i32 } %A, i32 11, 1 |
| 5467 |
// which allows the unused 0,0 element from the nested struct to be |
5467 |
// which allows the unused 0,0 element from the nested struct to be |
| 5468 |
// removed. |
5468 |
// removed. |
| 5469 |
return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx), |
5469 |
return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx), |
| 5470 |
InsertBefore); |
5470 |
InsertBefore); |
| 5471 |
} |
5471 |
} |
| 5472 |
|
5472 |
|
| 5473 |
// This insert value inserts something else than what we are looking for. |
5473 |
// This insert value inserts something else than what we are looking for. |
| 5474 |
// See if the (aggregate) value inserted into has the value we are |
5474 |
// See if the (aggregate) value inserted into has the value we are |
| 5475 |
// looking for, then. |
5475 |
// looking for, then. |
| 5476 |
if (*req_idx != *i) |
5476 |
if (*req_idx != *i) |
| 5477 |
return FindInsertedValue(I->getAggregateOperand(), idx_range, |
5477 |
return FindInsertedValue(I->getAggregateOperand(), idx_range, |
| 5478 |
InsertBefore); |
5478 |
InsertBefore); |
| 5479 |
} |
5479 |
} |
| 5480 |
// If we end up here, the indices of the insertvalue match with those |
5480 |
// If we end up here, the indices of the insertvalue match with those |
| 5481 |
// requested (though possibly only partially). Now we recursively look at |
5481 |
// requested (though possibly only partially). Now we recursively look at |
| 5482 |
// the inserted value, passing any remaining indices. |
5482 |
// the inserted value, passing any remaining indices. |
| 5483 |
return FindInsertedValue(I->getInsertedValueOperand(), |
5483 |
return FindInsertedValue(I->getInsertedValueOperand(), |
| 5484 |
ArrayRef(req_idx, idx_range.end()), InsertBefore); |
5484 |
ArrayRef(req_idx, idx_range.end()), InsertBefore); |
| 5485 |
} |
5485 |
} |
| 5486 |
|
5486 |
|
| 5487 |
if (ExtractValueInst *I = dyn_cast(V)) { |
5487 |
if (ExtractValueInst *I = dyn_cast(V)) { |
| 5488 |
// If we're extracting a value from an aggregate that was extracted from |
5488 |
// If we're extracting a value from an aggregate that was extracted from |
| 5489 |
// something else, we can extract from that something else directly instead. |
5489 |
// something else, we can extract from that something else directly instead. |
| 5490 |
// However, we will need to chain I's indices with the requested indices. |
5490 |
// However, we will need to chain I's indices with the requested indices. |
| 5491 |
|
5491 |
|
| 5492 |
// Calculate the number of indices required |
5492 |
// Calculate the number of indices required |
| 5493 |
unsigned size = I->getNumIndices() + idx_range.size(); |
5493 |
unsigned size = I->getNumIndices() + idx_range.size(); |
| 5494 |
// Allocate some space to put the new indices in |
5494 |
// Allocate some space to put the new indices in |
| 5495 |
SmallVector Idxs; |
5495 |
SmallVector Idxs; |
| 5496 |
Idxs.reserve(size); |
5496 |
Idxs.reserve(size); |
| 5497 |
// Add indices from the extract value instruction |
5497 |
// Add indices from the extract value instruction |
| 5498 |
Idxs.append(I->idx_begin(), I->idx_end()); |
5498 |
Idxs.append(I->idx_begin(), I->idx_end()); |
| 5499 |
|
5499 |
|
| 5500 |
// Add requested indices |
5500 |
// Add requested indices |
| 5501 |
Idxs.append(idx_range.begin(), idx_range.end()); |
5501 |
Idxs.append(idx_range.begin(), idx_range.end()); |
| 5502 |
|
5502 |
|
| 5503 |
assert(Idxs.size() == size |
5503 |
assert(Idxs.size() == size |
| 5504 |
&& "Number of indices added not correct?"); |
5504 |
&& "Number of indices added not correct?"); |
| 5505 |
|
5505 |
|
| 5506 |
return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore); |
5506 |
return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore); |
| 5507 |
} |
5507 |
} |
| 5508 |
// Otherwise, we don't know (such as, extracting from a function return value |
5508 |
// Otherwise, we don't know (such as, extracting from a function return value |
| 5509 |
// or load instruction) |
5509 |
// or load instruction) |
| 5510 |
return nullptr; |
5510 |
return nullptr; |
| 5511 |
} |
5511 |
} |
| 5512 |
|
5512 |
|
| 5513 |
bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP, |
5513 |
bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP, |
| 5514 |
unsigned CharSize) { |
5514 |
unsigned CharSize) { |
| 5515 |
// Make sure the GEP has exactly three arguments. |
5515 |
// Make sure the GEP has exactly three arguments. |
| 5516 |
if (GEP->getNumOperands() != 3) |
5516 |
if (GEP->getNumOperands() != 3) |
| 5517 |
return false; |
5517 |
return false; |
| 5518 |
|
5518 |
|
| 5519 |
// Make sure the index-ee is a pointer to array of \p CharSize integers. |
5519 |
// Make sure the index-ee is a pointer to array of \p CharSize integers. |
| 5520 |
// CharSize. |
5520 |
// CharSize. |
| 5521 |
ArrayType *AT = dyn_cast(GEP->getSourceElementType()); |
5521 |
ArrayType *AT = dyn_cast(GEP->getSourceElementType()); |
| 5522 |
if (!AT || !AT->getElementType()->isIntegerTy(CharSize)) |
5522 |
if (!AT || !AT->getElementType()->isIntegerTy(CharSize)) |
| 5523 |
return false; |
5523 |
return false; |
| 5524 |
|
5524 |
|
| 5525 |
// Check to make sure that the first operand of the GEP is an integer and |
5525 |
// Check to make sure that the first operand of the GEP is an integer and |
| 5526 |
// has value 0 so that we are sure we're indexing into the initializer. |
5526 |
// has value 0 so that we are sure we're indexing into the initializer. |
| 5527 |
const ConstantInt *FirstIdx = dyn_cast(GEP->getOperand(1)); |
5527 |
const ConstantInt *FirstIdx = dyn_cast(GEP->getOperand(1)); |
| 5528 |
if (!FirstIdx || !FirstIdx->isZero()) |
5528 |
if (!FirstIdx || !FirstIdx->isZero()) |
| 5529 |
return false; |
5529 |
return false; |
| 5530 |
|
5530 |
|
| 5531 |
return true; |
5531 |
return true; |
| 5532 |
} |
5532 |
} |
| 5533 |
|
5533 |
|
| 5534 |
// If V refers to an initialized global constant, set Slice either to |
5534 |
// If V refers to an initialized global constant, set Slice either to |
| 5535 |
// its initializer if the size of its elements equals ElementSize, or, |
5535 |
// its initializer if the size of its elements equals ElementSize, or, |
| 5536 |
// for ElementSize == 8, to its representation as an array of unsiged |
5536 |
// for ElementSize == 8, to its representation as an array of unsiged |
| 5537 |
// char. Return true on success. |
5537 |
// char. Return true on success. |
| 5538 |
// Offset is in the unit "nr of ElementSize sized elements". |
5538 |
// Offset is in the unit "nr of ElementSize sized elements". |
| 5539 |
bool llvm::getConstantDataArrayInfo(const Value *V, |
5539 |
bool llvm::getConstantDataArrayInfo(const Value *V, |
| 5540 |
ConstantDataArraySlice &Slice, |
5540 |
ConstantDataArraySlice &Slice, |
| 5541 |
unsigned ElementSize, uint64_t Offset) { |
5541 |
unsigned ElementSize, uint64_t Offset) { |
| 5542 |
assert(V && "V should not be null."); |
5542 |
assert(V && "V should not be null."); |
| 5543 |
assert((ElementSize % 8) == 0 && |
5543 |
assert((ElementSize % 8) == 0 && |
| 5544 |
"ElementSize expected to be a multiple of the size of a byte."); |
5544 |
"ElementSize expected to be a multiple of the size of a byte."); |
| 5545 |
unsigned ElementSizeInBytes = ElementSize / 8; |
5545 |
unsigned ElementSizeInBytes = ElementSize / 8; |
| 5546 |
|
5546 |
|
| 5547 |
// Drill down into the pointer expression V, ignoring any intervening |
5547 |
// Drill down into the pointer expression V, ignoring any intervening |
| 5548 |
// casts, and determine the identity of the object it references along |
5548 |
// casts, and determine the identity of the object it references along |
| 5549 |
// with the cumulative byte offset into it. |
5549 |
// with the cumulative byte offset into it. |
| 5550 |
const GlobalVariable *GV = |
5550 |
const GlobalVariable *GV = |
| 5551 |
dyn_cast(getUnderlyingObject(V)); |
5551 |
dyn_cast(getUnderlyingObject(V)); |
| 5552 |
if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) |
5552 |
if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) |
| 5553 |
// Fail if V is not based on constant global object. |
5553 |
// Fail if V is not based on constant global object. |
| 5554 |
return false; |
5554 |
return false; |
| 5555 |
|
5555 |
|
| 5556 |
const DataLayout &DL = GV->getParent()->getDataLayout(); |
5556 |
const DataLayout &DL = GV->getParent()->getDataLayout(); |
| 5557 |
APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0); |
5557 |
APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0); |
| 5558 |
|
5558 |
|
| 5559 |
if (GV != V->stripAndAccumulateConstantOffsets(DL, Off, |
5559 |
if (GV != V->stripAndAccumulateConstantOffsets(DL, Off, |
| 5560 |
/*AllowNonInbounds*/ true)) |
5560 |
/*AllowNonInbounds*/ true)) |
| 5561 |
// Fail if a constant offset could not be determined. |
5561 |
// Fail if a constant offset could not be determined. |
| 5562 |
return false; |
5562 |
return false; |
| 5563 |
|
5563 |
|
| 5564 |
uint64_t StartIdx = Off.getLimitedValue(); |
5564 |
uint64_t StartIdx = Off.getLimitedValue(); |
| 5565 |
if (StartIdx == UINT64_MAX) |
5565 |
if (StartIdx == UINT64_MAX) |
| 5566 |
// Fail if the constant offset is excessive. |
5566 |
// Fail if the constant offset is excessive. |
| 5567 |
return false; |
5567 |
return false; |
| 5568 |
|
5568 |
|
| 5569 |
// Off/StartIdx is in the unit of bytes. So we need to convert to number of |
5569 |
// Off/StartIdx is in the unit of bytes. So we need to convert to number of |
| 5570 |
// elements. Simply bail out if that isn't possible. |
5570 |
// elements. Simply bail out if that isn't possible. |
| 5571 |
if ((StartIdx % ElementSizeInBytes) != 0) |
5571 |
if ((StartIdx % ElementSizeInBytes) != 0) |
| 5572 |
return false; |
5572 |
return false; |
| 5573 |
|
5573 |
|
| 5574 |
Offset += StartIdx / ElementSizeInBytes; |
5574 |
Offset += StartIdx / ElementSizeInBytes; |
| 5575 |
ConstantDataArray *Array = nullptr; |
5575 |
ConstantDataArray *Array = nullptr; |
| 5576 |
ArrayType *ArrayTy = nullptr; |
5576 |
ArrayType *ArrayTy = nullptr; |
| 5577 |
|
5577 |
|
| 5578 |
if (GV->getInitializer()->isNullValue()) { |
5578 |
if (GV->getInitializer()->isNullValue()) { |
| 5579 |
Type *GVTy = GV->getValueType(); |
5579 |
Type *GVTy = GV->getValueType(); |
| 5580 |
uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue(); |
5580 |
uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue(); |
| 5581 |
uint64_t Length = SizeInBytes / ElementSizeInBytes; |
5581 |
uint64_t Length = SizeInBytes / ElementSizeInBytes; |
| 5582 |
|
5582 |
|
| 5583 |
Slice.Array = nullptr; |
5583 |
Slice.Array = nullptr; |
| 5584 |
Slice.Offset = 0; |
5584 |
Slice.Offset = 0; |
| 5585 |
// Return an empty Slice for undersized constants to let callers |
5585 |
// Return an empty Slice for undersized constants to let callers |
| 5586 |
// transform even undefined library calls into simpler, well-defined |
5586 |
// transform even undefined library calls into simpler, well-defined |
| 5587 |
// expressions. This is preferable to making the calls although it |
5587 |
// expressions. This is preferable to making the calls although it |
| 5588 |
// prevents sanitizers from detecting such calls. |
5588 |
// prevents sanitizers from detecting such calls. |
| 5589 |
Slice.Length = Length < Offset ? 0 : Length - Offset; |
5589 |
Slice.Length = Length < Offset ? 0 : Length - Offset; |
| 5590 |
return true; |
5590 |
return true; |
| 5591 |
} |
5591 |
} |
| 5592 |
|
5592 |
|
| 5593 |
auto *Init = const_cast(GV->getInitializer()); |
5593 |
auto *Init = const_cast(GV->getInitializer()); |
| 5594 |
if (auto *ArrayInit = dyn_cast(Init)) { |
5594 |
if (auto *ArrayInit = dyn_cast(Init)) { |
| 5595 |
Type *InitElTy = ArrayInit->getElementType(); |
5595 |
Type *InitElTy = ArrayInit->getElementType(); |
| 5596 |
if (InitElTy->isIntegerTy(ElementSize)) { |
5596 |
if (InitElTy->isIntegerTy(ElementSize)) { |
| 5597 |
// If Init is an initializer for an array of the expected type |
5597 |
// If Init is an initializer for an array of the expected type |
| 5598 |
// and size, use it as is. |
5598 |
// and size, use it as is. |
| 5599 |
Array = ArrayInit; |
5599 |
Array = ArrayInit; |
| 5600 |
ArrayTy = ArrayInit->getType(); |
5600 |
ArrayTy = ArrayInit->getType(); |
| 5601 |
} |
5601 |
} |
| 5602 |
} |
5602 |
} |
| 5603 |
|
5603 |
|
| 5604 |
if (!Array) { |
5604 |
if (!Array) { |
| 5605 |
if (ElementSize != 8) |
5605 |
if (ElementSize != 8) |
| 5606 |
// TODO: Handle conversions to larger integral types. |
5606 |
// TODO: Handle conversions to larger integral types. |
| 5607 |
return false; |
5607 |
return false; |
| 5608 |
|
5608 |
|
| 5609 |
// Otherwise extract the portion of the initializer starting |
5609 |
// Otherwise extract the portion of the initializer starting |
| 5610 |
// at Offset as an array of bytes, and reset Offset. |
5610 |
// at Offset as an array of bytes, and reset Offset. |
| 5611 |
Init = ReadByteArrayFromGlobal(GV, Offset); |
5611 |
Init = ReadByteArrayFromGlobal(GV, Offset); |
| 5612 |
if (!Init) |
5612 |
if (!Init) |
| 5613 |
return false; |
5613 |
return false; |
| 5614 |
|
5614 |
|
| 5615 |
Offset = 0; |
5615 |
Offset = 0; |
| 5616 |
Array = dyn_cast(Init); |
5616 |
Array = dyn_cast(Init); |
| 5617 |
ArrayTy = dyn_cast(Init->getType()); |
5617 |
ArrayTy = dyn_cast(Init->getType()); |
| 5618 |
} |
5618 |
} |
| 5619 |
|
5619 |
|
| 5620 |
uint64_t NumElts = ArrayTy->getArrayNumElements(); |
5620 |
uint64_t NumElts = ArrayTy->getArrayNumElements(); |
| 5621 |
if (Offset > NumElts) |
5621 |
if (Offset > NumElts) |
| 5622 |
return false; |
5622 |
return false; |
| 5623 |
|
5623 |
|
| 5624 |
Slice.Array = Array; |
5624 |
Slice.Array = Array; |
| 5625 |
Slice.Offset = Offset; |
5625 |
Slice.Offset = Offset; |
| 5626 |
Slice.Length = NumElts - Offset; |
5626 |
Slice.Length = NumElts - Offset; |
| 5627 |
return true; |
5627 |
return true; |
| 5628 |
} |
5628 |
} |
| 5629 |
|
5629 |
|
| 5630 |
/// Extract bytes from the initializer of the constant array V, which need |
5630 |
/// Extract bytes from the initializer of the constant array V, which need |
| 5631 |
/// not be a nul-terminated string. On success, store the bytes in Str and |
5631 |
/// not be a nul-terminated string. On success, store the bytes in Str and |
| 5632 |
/// return true. When TrimAtNul is set, Str will contain only the bytes up |
5632 |
/// return true. When TrimAtNul is set, Str will contain only the bytes up |
| 5633 |
/// to but not including the first nul. Return false on failure. |
5633 |
/// to but not including the first nul. Return false on failure. |
| 5634 |
bool llvm::getConstantStringInfo(const Value *V, StringRef &Str, |
5634 |
bool llvm::getConstantStringInfo(const Value *V, StringRef &Str, |
| 5635 |
bool TrimAtNul) { |
5635 |
bool TrimAtNul) { |
| 5636 |
ConstantDataArraySlice Slice; |
5636 |
ConstantDataArraySlice Slice; |
| 5637 |
if (!getConstantDataArrayInfo(V, Slice, 8)) |
5637 |
if (!getConstantDataArrayInfo(V, Slice, 8)) |
| 5638 |
return false; |
5638 |
return false; |
| 5639 |
|
5639 |
|
| 5640 |
if (Slice.Array == nullptr) { |
5640 |
if (Slice.Array == nullptr) { |
| 5641 |
if (TrimAtNul) { |
5641 |
if (TrimAtNul) { |
| 5642 |
// Return a nul-terminated string even for an empty Slice. This is |
5642 |
// Return a nul-terminated string even for an empty Slice. This is |
| 5643 |
// safe because all existing SimplifyLibcalls callers require string |
5643 |
// safe because all existing SimplifyLibcalls callers require string |
| 5644 |
// arguments and the behavior of the functions they fold is undefined |
5644 |
// arguments and the behavior of the functions they fold is undefined |
| 5645 |
// otherwise. Folding the calls this way is preferable to making |
5645 |
// otherwise. Folding the calls this way is preferable to making |
| 5646 |
// the undefined library calls, even though it prevents sanitizers |
5646 |
// the undefined library calls, even though it prevents sanitizers |
| 5647 |
// from reporting such calls. |
5647 |
// from reporting such calls. |
| 5648 |
Str = StringRef(); |
5648 |
Str = StringRef(); |
| 5649 |
return true; |
5649 |
return true; |
| 5650 |
} |
5650 |
} |
| 5651 |
if (Slice.Length == 1) { |
5651 |
if (Slice.Length == 1) { |
| 5652 |
Str = StringRef("", 1); |
5652 |
Str = StringRef("", 1); |
| 5653 |
return true; |
5653 |
return true; |
| 5654 |
} |
5654 |
} |
| 5655 |
// We cannot instantiate a StringRef as we do not have an appropriate string |
5655 |
// We cannot instantiate a StringRef as we do not have an appropriate string |
| 5656 |
// of 0s at hand. |
5656 |
// of 0s at hand. |
| 5657 |
return false; |
5657 |
return false; |
| 5658 |
} |
5658 |
} |
| 5659 |
|
5659 |
|
| 5660 |
// Start out with the entire array in the StringRef. |
5660 |
// Start out with the entire array in the StringRef. |
| 5661 |
Str = Slice.Array->getAsString(); |
5661 |
Str = Slice.Array->getAsString(); |
| 5662 |
// Skip over 'offset' bytes. |
5662 |
// Skip over 'offset' bytes. |
| 5663 |
Str = Str.substr(Slice.Offset); |
5663 |
Str = Str.substr(Slice.Offset); |
| 5664 |
|
5664 |
|
| 5665 |
if (TrimAtNul) { |
5665 |
if (TrimAtNul) { |
| 5666 |
// Trim off the \0 and anything after it. If the array is not nul |
5666 |
// Trim off the \0 and anything after it. If the array is not nul |
| 5667 |
// terminated, we just return the whole end of string. The client may know |
5667 |
// terminated, we just return the whole end of string. The client may know |
| 5668 |
// some other way that the string is length-bound. |
5668 |
// some other way that the string is length-bound. |
| 5669 |
Str = Str.substr(0, Str.find('\0')); |
5669 |
Str = Str.substr(0, Str.find('\0')); |
| 5670 |
} |
5670 |
} |
| 5671 |
return true; |
5671 |
return true; |
| 5672 |
} |
5672 |
} |
| 5673 |
|
5673 |
|
| 5674 |
// These next two are very similar to the above, but also look through PHI |
5674 |
// These next two are very similar to the above, but also look through PHI |
| 5675 |
// nodes. |
5675 |
// nodes. |
| 5676 |
// TODO: See if we can integrate these two together. |
5676 |
// TODO: See if we can integrate these two together. |
| 5677 |
|
5677 |
|
| 5678 |
/// If we can compute the length of the string pointed to by |
5678 |
/// If we can compute the length of the string pointed to by |
| 5679 |
/// the specified pointer, return 'len+1'. If we can't, return 0. |
5679 |
/// the specified pointer, return 'len+1'. If we can't, return 0. |
| 5680 |
static uint64_t GetStringLengthH(const Value *V, |
5680 |
static uint64_t GetStringLengthH(const Value *V, |
| 5681 |
SmallPtrSetImpl &PHIs, |
5681 |
SmallPtrSetImpl &PHIs, |
| 5682 |
unsigned CharSize) { |
5682 |
unsigned CharSize) { |
| 5683 |
// Look through noop bitcast instructions. |
5683 |
// Look through noop bitcast instructions. |
| 5684 |
V = V->stripPointerCasts(); |
5684 |
V = V->stripPointerCasts(); |
| 5685 |
|
5685 |
|
| 5686 |
// If this is a PHI node, there are two cases: either we have already seen it |
5686 |
// If this is a PHI node, there are two cases: either we have already seen it |
| 5687 |
// or we haven't. |
5687 |
// or we haven't. |
| 5688 |
if (const PHINode *PN = dyn_cast(V)) { |
5688 |
if (const PHINode *PN = dyn_cast(V)) { |
| 5689 |
if (!PHIs.insert(PN).second) |
5689 |
if (!PHIs.insert(PN).second) |
| 5690 |
return ~0ULL; // already in the set. |
5690 |
return ~0ULL; // already in the set. |
| 5691 |
|
5691 |
|
| 5692 |
// If it was new, see if all the input strings are the same length. |
5692 |
// If it was new, see if all the input strings are the same length. |
| 5693 |
uint64_t LenSoFar = ~0ULL; |
5693 |
uint64_t LenSoFar = ~0ULL; |
| 5694 |
for (Value *IncValue : PN->incoming_values()) { |
5694 |
for (Value *IncValue : PN->incoming_values()) { |
| 5695 |
uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize); |
5695 |
uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize); |
| 5696 |
if (Len == 0) return 0; // Unknown length -> unknown. |
5696 |
if (Len == 0) return 0; // Unknown length -> unknown. |
| 5697 |
|
5697 |
|
| 5698 |
if (Len == ~0ULL) continue; |
5698 |
if (Len == ~0ULL) continue; |
| 5699 |
|
5699 |
|
| 5700 |
if (Len != LenSoFar && LenSoFar != ~0ULL) |
5700 |
if (Len != LenSoFar && LenSoFar != ~0ULL) |
| 5701 |
return 0; // Disagree -> unknown. |
5701 |
return 0; // Disagree -> unknown. |
| 5702 |
LenSoFar = Len; |
5702 |
LenSoFar = Len; |
| 5703 |
} |
5703 |
} |
| 5704 |
|
5704 |
|
| 5705 |
// Success, all agree. |
5705 |
// Success, all agree. |
| 5706 |
return LenSoFar; |
5706 |
return LenSoFar; |
| 5707 |
} |
5707 |
} |
| 5708 |
|
5708 |
|
| 5709 |
// strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) |
5709 |
// strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) |
| 5710 |
if (const SelectInst *SI = dyn_cast(V)) { |
5710 |
if (const SelectInst *SI = dyn_cast(V)) { |
| 5711 |
uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize); |
5711 |
uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize); |
| 5712 |
if (Len1 == 0) return 0; |
5712 |
if (Len1 == 0) return 0; |
| 5713 |
uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize); |
5713 |
uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize); |
| 5714 |
if (Len2 == 0) return 0; |
5714 |
if (Len2 == 0) return 0; |
| 5715 |
if (Len1 == ~0ULL) return Len2; |
5715 |
if (Len1 == ~0ULL) return Len2; |
| 5716 |
if (Len2 == ~0ULL) return Len1; |
5716 |
if (Len2 == ~0ULL) return Len1; |
| 5717 |
if (Len1 != Len2) return 0; |
5717 |
if (Len1 != Len2) return 0; |
| 5718 |
return Len1; |
5718 |
return Len1; |
| 5719 |
} |
5719 |
} |
| 5720 |
|
5720 |
|
| 5721 |
// Otherwise, see if we can read the string. |
5721 |
// Otherwise, see if we can read the string. |
| 5722 |
ConstantDataArraySlice Slice; |
5722 |
ConstantDataArraySlice Slice; |
| 5723 |
if (!getConstantDataArrayInfo(V, Slice, CharSize)) |
5723 |
if (!getConstantDataArrayInfo(V, Slice, CharSize)) |
| 5724 |
return 0; |
5724 |
return 0; |
| 5725 |
|
5725 |
|
| 5726 |
if (Slice.Array == nullptr) |
5726 |
if (Slice.Array == nullptr) |
| 5727 |
// Zeroinitializer (including an empty one). |
5727 |
// Zeroinitializer (including an empty one). |
| 5728 |
return 1; |
5728 |
return 1; |
| 5729 |
|
5729 |
|
| 5730 |
// Search for the first nul character. Return a conservative result even |
5730 |
// Search for the first nul character. Return a conservative result even |
| 5731 |
// when there is no nul. This is safe since otherwise the string function |
5731 |
// when there is no nul. This is safe since otherwise the string function |
| 5732 |
// being folded such as strlen is undefined, and can be preferable to |
5732 |
// being folded such as strlen is undefined, and can be preferable to |
| 5733 |
// making the undefined library call. |
5733 |
// making the undefined library call. |
| 5734 |
unsigned NullIndex = 0; |
5734 |
unsigned NullIndex = 0; |
| 5735 |
for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) { |
5735 |
for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) { |
| 5736 |
if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0) |
5736 |
if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0) |
| 5737 |
break; |
5737 |
break; |
| 5738 |
} |
5738 |
} |
| 5739 |
|
5739 |
|
| 5740 |
return NullIndex + 1; |
5740 |
return NullIndex + 1; |
| 5741 |
} |
5741 |
} |
| 5742 |
|
5742 |
|
| 5743 |
/// If we can compute the length of the string pointed to by |
5743 |
/// If we can compute the length of the string pointed to by |
| 5744 |
/// the specified pointer, return 'len+1'. If we can't, return 0. |
5744 |
/// the specified pointer, return 'len+1'. If we can't, return 0. |
| 5745 |
uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) { |
5745 |
uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) { |
| 5746 |
if (!V->getType()->isPointerTy()) |
5746 |
if (!V->getType()->isPointerTy()) |
| 5747 |
return 0; |
5747 |
return 0; |
| 5748 |
|
5748 |
|
| 5749 |
SmallPtrSet PHIs; |
5749 |
SmallPtrSet PHIs; |
| 5750 |
uint64_t Len = GetStringLengthH(V, PHIs, CharSize); |
5750 |
uint64_t Len = GetStringLengthH(V, PHIs, CharSize); |
| 5751 |
// If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return |
5751 |
// If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return |
| 5752 |
// an empty string as a length. |
5752 |
// an empty string as a length. |
| 5753 |
return Len == ~0ULL ? 1 : Len; |
5753 |
return Len == ~0ULL ? 1 : Len; |
| 5754 |
} |
5754 |
} |
| 5755 |
|
5755 |
|
| 5756 |
const Value * |
5756 |
const Value * |
| 5757 |
llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call, |
5757 |
llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call, |
| 5758 |
bool MustPreserveNullness) { |
5758 |
bool MustPreserveNullness) { |
| 5759 |
assert(Call && |
5759 |
assert(Call && |
| 5760 |
"getArgumentAliasingToReturnedPointer only works on nonnull calls"); |
5760 |
"getArgumentAliasingToReturnedPointer only works on nonnull calls"); |
| 5761 |
if (const Value *RV = Call->getReturnedArgOperand()) |
5761 |
if (const Value *RV = Call->getReturnedArgOperand()) |
| 5762 |
return RV; |
5762 |
return RV; |
| 5763 |
// This can be used only as a aliasing property. |
5763 |
// This can be used only as a aliasing property. |
| 5764 |
if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( |
5764 |
if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( |
| 5765 |
Call, MustPreserveNullness)) |
5765 |
Call, MustPreserveNullness)) |
| 5766 |
return Call->getArgOperand(0); |
5766 |
return Call->getArgOperand(0); |
| 5767 |
return nullptr; |
5767 |
return nullptr; |
| 5768 |
} |
5768 |
} |
| 5769 |
|
5769 |
|
| 5770 |
bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( |
5770 |
bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( |
| 5771 |
const CallBase *Call, bool MustPreserveNullness) { |
5771 |
const CallBase *Call, bool MustPreserveNullness) { |
| 5772 |
switch (Call->getIntrinsicID()) { |
5772 |
switch (Call->getIntrinsicID()) { |
| 5773 |
case Intrinsic::launder_invariant_group: |
5773 |
case Intrinsic::launder_invariant_group: |
| 5774 |
case Intrinsic::strip_invariant_group: |
5774 |
case Intrinsic::strip_invariant_group: |
| 5775 |
case Intrinsic::aarch64_irg: |
5775 |
case Intrinsic::aarch64_irg: |
| 5776 |
case Intrinsic::aarch64_tagp: |
5776 |
case Intrinsic::aarch64_tagp: |
| 5777 |
// The amdgcn_make_buffer_rsrc function does not alter the address of the |
5777 |
// The amdgcn_make_buffer_rsrc function does not alter the address of the |
| 5778 |
// input pointer (and thus preserve null-ness for the purposes of escape |
5778 |
// input pointer (and thus preserve null-ness for the purposes of escape |
| 5779 |
// analysis, which is where the MustPreserveNullness flag comes in to play). |
5779 |
// analysis, which is where the MustPreserveNullness flag comes in to play). |
| 5780 |
// However, it will not necessarily map ptr addrspace(N) null to ptr |
5780 |
// However, it will not necessarily map ptr addrspace(N) null to ptr |
| 5781 |
// addrspace(8) null, aka the "null descriptor", which has "all loads return |
5781 |
// addrspace(8) null, aka the "null descriptor", which has "all loads return |
| 5782 |
// 0, all stores are dropped" semantics. Given the context of this intrinsic |
5782 |
// 0, all stores are dropped" semantics. Given the context of this intrinsic |
| 5783 |
// list, no one should be relying on such a strict interpretation of |
5783 |
// list, no one should be relying on such a strict interpretation of |
| 5784 |
// MustPreserveNullness (and, at time of writing, they are not), but we |
5784 |
// MustPreserveNullness (and, at time of writing, they are not), but we |
| 5785 |
// document this fact out of an abundance of caution. |
5785 |
// document this fact out of an abundance of caution. |
| 5786 |
case Intrinsic::amdgcn_make_buffer_rsrc: |
5786 |
case Intrinsic::amdgcn_make_buffer_rsrc: |
| 5787 |
return true; |
5787 |
return true; |
| 5788 |
case Intrinsic::ptrmask: |
5788 |
case Intrinsic::ptrmask: |
| 5789 |
return !MustPreserveNullness; |
5789 |
return !MustPreserveNullness; |
| 5790 |
default: |
5790 |
default: |
| 5791 |
return false; |
5791 |
return false; |
| 5792 |
} |
5792 |
} |
| 5793 |
} |
5793 |
} |
| 5794 |
|
5794 |
|
| 5795 |
/// \p PN defines a loop-variant pointer to an object. Check if the |
5795 |
/// \p PN defines a loop-variant pointer to an object. Check if the |
| 5796 |
/// previous iteration of the loop was referring to the same object as \p PN. |
5796 |
/// previous iteration of the loop was referring to the same object as \p PN. |
| 5797 |
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, |
5797 |
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, |
| 5798 |
const LoopInfo *LI) { |
5798 |
const LoopInfo *LI) { |
| 5799 |
// Find the loop-defined value. |
5799 |
// Find the loop-defined value. |
| 5800 |
Loop *L = LI->getLoopFor(PN->getParent()); |
5800 |
Loop *L = LI->getLoopFor(PN->getParent()); |
| 5801 |
if (PN->getNumIncomingValues() != 2) |
5801 |
if (PN->getNumIncomingValues() != 2) |
| 5802 |
return true; |
5802 |
return true; |
| 5803 |
|
5803 |
|
| 5804 |
// Find the value from previous iteration. |
5804 |
// Find the value from previous iteration. |
| 5805 |
auto *PrevValue = dyn_cast(PN->getIncomingValue(0)); |
5805 |
auto *PrevValue = dyn_cast(PN->getIncomingValue(0)); |
| 5806 |
if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
5806 |
if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 5807 |
PrevValue = dyn_cast(PN->getIncomingValue(1)); |
5807 |
PrevValue = dyn_cast(PN->getIncomingValue(1)); |
| 5808 |
if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
5808 |
if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 5809 |
return true; |
5809 |
return true; |
| 5810 |
|
5810 |
|
| 5811 |
// If a new pointer is loaded in the loop, the pointer references a different |
5811 |
// If a new pointer is loaded in the loop, the pointer references a different |
| 5812 |
// object in every iteration. E.g.: |
5812 |
// object in every iteration. E.g.: |
| 5813 |
// for (i) |
5813 |
// for (i) |
| 5814 |
// int *p = a[i]; |
5814 |
// int *p = a[i]; |
| 5815 |
// ... |
5815 |
// ... |
| 5816 |
if (auto *Load = dyn_cast(PrevValue)) |
5816 |
if (auto *Load = dyn_cast(PrevValue)) |
| 5817 |
if (!L->isLoopInvariant(Load->getPointerOperand())) |
5817 |
if (!L->isLoopInvariant(Load->getPointerOperand())) |
| 5818 |
return false; |
5818 |
return false; |
| 5819 |
return true; |
5819 |
return true; |
| 5820 |
} |
5820 |
} |
| 5821 |
|
5821 |
|
| 5822 |
const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) { |
5822 |
const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) { |
| 5823 |
if (!V->getType()->isPointerTy()) |
5823 |
if (!V->getType()->isPointerTy()) |
| 5824 |
return V; |
5824 |
return V; |
| 5825 |
for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { |
5825 |
for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { |
| 5826 |
if (auto *GEP = dyn_cast(V)) { |
5826 |
if (auto *GEP = dyn_cast(V)) { |
| 5827 |
V = GEP->getPointerOperand(); |
5827 |
V = GEP->getPointerOperand(); |
| 5828 |
} else if (Operator::getOpcode(V) == Instruction::BitCast || |
5828 |
} else if (Operator::getOpcode(V) == Instruction::BitCast || |
| 5829 |
Operator::getOpcode(V) == Instruction::AddrSpaceCast) { |
5829 |
Operator::getOpcode(V) == Instruction::AddrSpaceCast) { |
| 5830 |
V = cast(V)->getOperand(0); |
5830 |
V = cast(V)->getOperand(0); |
| 5831 |
if (!V->getType()->isPointerTy()) |
5831 |
if (!V->getType()->isPointerTy()) |
| 5832 |
return V; |
5832 |
return V; |
| 5833 |
} else if (auto *GA = dyn_cast(V)) { |
5833 |
} else if (auto *GA = dyn_cast(V)) { |
| 5834 |
if (GA->isInterposable()) |
5834 |
if (GA->isInterposable()) |
| 5835 |
return V; |
5835 |
return V; |
| 5836 |
V = GA->getAliasee(); |
5836 |
V = GA->getAliasee(); |
| 5837 |
} else { |
5837 |
} else { |
| 5838 |
if (auto *PHI = dyn_cast(V)) { |
5838 |
if (auto *PHI = dyn_cast(V)) { |
| 5839 |
// Look through single-arg phi nodes created by LCSSA. |
5839 |
// Look through single-arg phi nodes created by LCSSA. |
| 5840 |
if (PHI->getNumIncomingValues() == 1) { |
5840 |
if (PHI->getNumIncomingValues() == 1) { |
| 5841 |
V = PHI->getIncomingValue(0); |
5841 |
V = PHI->getIncomingValue(0); |
| 5842 |
continue; |
5842 |
continue; |
| 5843 |
} |
5843 |
} |
| 5844 |
} else if (auto *Call = dyn_cast(V)) { |
5844 |
} else if (auto *Call = dyn_cast(V)) { |
| 5845 |
// CaptureTracking can know about special capturing properties of some |
5845 |
// CaptureTracking can know about special capturing properties of some |
| 5846 |
// intrinsics like launder.invariant.group, that can't be expressed with |
5846 |
// intrinsics like launder.invariant.group, that can't be expressed with |
| 5847 |
// the attributes, but have properties like returning aliasing pointer. |
5847 |
// the attributes, but have properties like returning aliasing pointer. |
| 5848 |
// Because some analysis may assume that nocaptured pointer is not |
5848 |
// Because some analysis may assume that nocaptured pointer is not |
| 5849 |
// returned from some special intrinsic (because function would have to |
5849 |
// returned from some special intrinsic (because function would have to |
| 5850 |
// be marked with returns attribute), it is crucial to use this function |
5850 |
// be marked with returns attribute), it is crucial to use this function |
| 5851 |
// because it should be in sync with CaptureTracking. Not using it may |
5851 |
// because it should be in sync with CaptureTracking. Not using it may |
| 5852 |
// cause weird miscompilations where 2 aliasing pointers are assumed to |
5852 |
// cause weird miscompilations where 2 aliasing pointers are assumed to |
| 5853 |
// noalias. |
5853 |
// noalias. |
| 5854 |
if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) { |
5854 |
if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) { |
| 5855 |
V = RP; |
5855 |
V = RP; |
| 5856 |
continue; |
5856 |
continue; |
| 5857 |
} |
5857 |
} |
| 5858 |
} |
5858 |
} |
| 5859 |
|
5859 |
|
| 5860 |
return V; |
5860 |
return V; |
| 5861 |
} |
5861 |
} |
| 5862 |
assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
5862 |
assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
| 5863 |
} |
5863 |
} |
| 5864 |
return V; |
5864 |
return V; |
| 5865 |
} |
5865 |
} |
| 5866 |
|
5866 |
|
| 5867 |
void llvm::getUnderlyingObjects(const Value *V, |
5867 |
void llvm::getUnderlyingObjects(const Value *V, |
| 5868 |
SmallVectorImpl &Objects, |
5868 |
SmallVectorImpl &Objects, |
| 5869 |
LoopInfo *LI, unsigned MaxLookup) { |
5869 |
LoopInfo *LI, unsigned MaxLookup) { |
| 5870 |
SmallPtrSet Visited; |
5870 |
SmallPtrSet Visited; |
| 5871 |
SmallVector Worklist; |
5871 |
SmallVector Worklist; |
| 5872 |
Worklist.push_back(V); |
5872 |
Worklist.push_back(V); |
| 5873 |
do { |
5873 |
do { |
| 5874 |
const Value *P = Worklist.pop_back_val(); |
5874 |
const Value *P = Worklist.pop_back_val(); |
| 5875 |
P = getUnderlyingObject(P, MaxLookup); |
5875 |
P = getUnderlyingObject(P, MaxLookup); |
| 5876 |
|
5876 |
|
| 5877 |
if (!Visited.insert(P).second) |
5877 |
if (!Visited.insert(P).second) |
| 5878 |
continue; |
5878 |
continue; |
| 5879 |
|
5879 |
|
| 5880 |
if (auto *SI = dyn_cast(P)) { |
5880 |
if (auto *SI = dyn_cast(P)) { |
| 5881 |
Worklist.push_back(SI->getTrueValue()); |
5881 |
Worklist.push_back(SI->getTrueValue()); |
| 5882 |
Worklist.push_back(SI->getFalseValue()); |
5882 |
Worklist.push_back(SI->getFalseValue()); |
| 5883 |
continue; |
5883 |
continue; |
| 5884 |
} |
5884 |
} |
| 5885 |
|
5885 |
|
| 5886 |
if (auto *PN = dyn_cast(P)) { |
5886 |
if (auto *PN = dyn_cast(P)) { |
| 5887 |
// If this PHI changes the underlying object in every iteration of the |
5887 |
// If this PHI changes the underlying object in every iteration of the |
| 5888 |
// loop, don't look through it. Consider: |
5888 |
// loop, don't look through it. Consider: |
| 5889 |
// int **A; |
5889 |
// int **A; |
| 5890 |
// for (i) { |
5890 |
// for (i) { |
| 5891 |
// Prev = Curr; // Prev = PHI (Prev_0, Curr) |
5891 |
// Prev = Curr; // Prev = PHI (Prev_0, Curr) |
| 5892 |
// Curr = A[i]; |
5892 |
// Curr = A[i]; |
| 5893 |
// *Prev, *Curr; |
5893 |
// *Prev, *Curr; |
| 5894 |
// |
5894 |
// |
| 5895 |
// Prev is tracking Curr one iteration behind so they refer to different |
5895 |
// Prev is tracking Curr one iteration behind so they refer to different |
| 5896 |
// underlying objects. |
5896 |
// underlying objects. |
| 5897 |
if (!LI || !LI->isLoopHeader(PN->getParent()) || |
5897 |
if (!LI || !LI->isLoopHeader(PN->getParent()) || |
| 5898 |
isSameUnderlyingObjectInLoop(PN, LI)) |
5898 |
isSameUnderlyingObjectInLoop(PN, LI)) |
| 5899 |
append_range(Worklist, PN->incoming_values()); |
5899 |
append_range(Worklist, PN->incoming_values()); |
| 5900 |
continue; |
5900 |
continue; |
| 5901 |
} |
5901 |
} |
| 5902 |
|
5902 |
|
| 5903 |
Objects.push_back(P); |
5903 |
Objects.push_back(P); |
| 5904 |
} while (!Worklist.empty()); |
5904 |
} while (!Worklist.empty()); |
| 5905 |
} |
5905 |
} |
| 5906 |
|
5906 |
|
| 5907 |
/// This is the function that does the work of looking through basic |
5907 |
/// This is the function that does the work of looking through basic |
| 5908 |
/// ptrtoint+arithmetic+inttoptr sequences. |
5908 |
/// ptrtoint+arithmetic+inttoptr sequences. |
| 5909 |
static const Value *getUnderlyingObjectFromInt(const Value *V) { |
5909 |
static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 5910 |
do { |
5910 |
do { |
| 5911 |
if (const Operator *U = dyn_cast(V)) { |
5911 |
if (const Operator *U = dyn_cast(V)) { |
| 5912 |
// If we find a ptrtoint, we can transfer control back to the |
5912 |
// If we find a ptrtoint, we can transfer control back to the |
| 5913 |
// regular getUnderlyingObjectFromInt. |
5913 |
// regular getUnderlyingObjectFromInt. |
| 5914 |
if (U->getOpcode() == Instruction::PtrToInt) |
5914 |
if (U->getOpcode() == Instruction::PtrToInt) |
| 5915 |
return U->getOperand(0); |
5915 |
return U->getOperand(0); |
| 5916 |
// If we find an add of a constant, a multiplied value, or a phi, it's |
5916 |
// If we find an add of a constant, a multiplied value, or a phi, it's |
| 5917 |
// likely that the other operand will lead us to the base |
5917 |
// likely that the other operand will lead us to the base |
| 5918 |
// object. We don't have to worry about the case where the |
5918 |
// object. We don't have to worry about the case where the |
| 5919 |
// object address is somehow being computed by the multiply, |
5919 |
// object address is somehow being computed by the multiply, |
| 5920 |
// because our callers only care when the result is an |
5920 |
// because our callers only care when the result is an |
| 5921 |
// identifiable object. |
5921 |
// identifiable object. |
| 5922 |
if (U->getOpcode() != Instruction::Add || |
5922 |
if (U->getOpcode() != Instruction::Add || |
| 5923 |
(!isa(U->getOperand(1)) && |
5923 |
(!isa(U->getOperand(1)) && |
| 5924 |
Operator::getOpcode(U->getOperand(1)) != Instruction::Mul && |
5924 |
Operator::getOpcode(U->getOperand(1)) != Instruction::Mul && |
| 5925 |
!isa(U->getOperand(1)))) |
5925 |
!isa(U->getOperand(1)))) |
| 5926 |
return V; |
5926 |
return V; |
| 5927 |
V = U->getOperand(0); |
5927 |
V = U->getOperand(0); |
| 5928 |
} else { |
5928 |
} else { |
| 5929 |
return V; |
5929 |
return V; |
| 5930 |
} |
5930 |
} |
| 5931 |
assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
5931 |
assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
| 5932 |
} while (true); |
5932 |
} while (true); |
| 5933 |
} |
5933 |
} |
| 5934 |
|
5934 |
|
| 5935 |
/// This is a wrapper around getUnderlyingObjects and adds support for basic |
5935 |
/// This is a wrapper around getUnderlyingObjects and adds support for basic |
| 5936 |
/// ptrtoint+arithmetic+inttoptr sequences. |
5936 |
/// ptrtoint+arithmetic+inttoptr sequences. |
| 5937 |
/// It returns false if unidentified object is found in getUnderlyingObjects. |
5937 |
/// It returns false if unidentified object is found in getUnderlyingObjects. |
| 5938 |
bool llvm::getUnderlyingObjectsForCodeGen(const Value *V, |
5938 |
bool llvm::getUnderlyingObjectsForCodeGen(const Value *V, |
| 5939 |
SmallVectorImpl &Objects) { |
5939 |
SmallVectorImpl &Objects) { |
| 5940 |
SmallPtrSet Visited; |
5940 |
SmallPtrSet Visited; |
| 5941 |
SmallVector Working(1, V); |
5941 |
SmallVector Working(1, V); |
| 5942 |
do { |
5942 |
do { |
| 5943 |
V = Working.pop_back_val(); |
5943 |
V = Working.pop_back_val(); |
| 5944 |
|
5944 |
|
| 5945 |
SmallVector Objs; |
5945 |
SmallVector Objs; |
| 5946 |
getUnderlyingObjects(V, Objs); |
5946 |
getUnderlyingObjects(V, Objs); |
| 5947 |
|
5947 |
|
| 5948 |
for (const Value *V : Objs) { |
5948 |
for (const Value *V : Objs) { |
| 5949 |
if (!Visited.insert(V).second) |
5949 |
if (!Visited.insert(V).second) |
| 5950 |
continue; |
5950 |
continue; |
| 5951 |
if (Operator::getOpcode(V) == Instruction::IntToPtr) { |
5951 |
if (Operator::getOpcode(V) == Instruction::IntToPtr) { |
| 5952 |
const Value *O = |
5952 |
const Value *O = |
| 5953 |
getUnderlyingObjectFromInt(cast(V)->getOperand(0)); |
5953 |
getUnderlyingObjectFromInt(cast(V)->getOperand(0)); |
| 5954 |
if (O->getType()->isPointerTy()) { |
5954 |
if (O->getType()->isPointerTy()) { |
| 5955 |
Working.push_back(O); |
5955 |
Working.push_back(O); |
| 5956 |
continue; |
5956 |
continue; |
| 5957 |
} |
5957 |
} |
| 5958 |
} |
5958 |
} |
| 5959 |
// If getUnderlyingObjects fails to find an identifiable object, |
5959 |
// If getUnderlyingObjects fails to find an identifiable object, |
| 5960 |
// getUnderlyingObjectsForCodeGen also fails for safety. |
5960 |
// getUnderlyingObjectsForCodeGen also fails for safety. |
| 5961 |
if (!isIdentifiedObject(V)) { |
5961 |
if (!isIdentifiedObject(V)) { |
| 5962 |
Objects.clear(); |
5962 |
Objects.clear(); |
| 5963 |
return false; |
5963 |
return false; |
| 5964 |
} |
5964 |
} |
| 5965 |
Objects.push_back(const_cast(V)); |
5965 |
Objects.push_back(const_cast(V)); |
| 5966 |
} |
5966 |
} |
| 5967 |
} while (!Working.empty()); |
5967 |
} while (!Working.empty()); |
| 5968 |
return true; |
5968 |
return true; |
| 5969 |
} |
5969 |
} |
| 5970 |
|
5970 |
|
| 5971 |
AllocaInst *llvm::findAllocaForValue(Value *V, bool OffsetZero) { |
5971 |
AllocaInst *llvm::findAllocaForValue(Value *V, bool OffsetZero) { |
| 5972 |
AllocaInst *Result = nullptr; |
5972 |
AllocaInst *Result = nullptr; |
| 5973 |
SmallPtrSet Visited; |
5973 |
SmallPtrSet Visited; |
| 5974 |
SmallVector Worklist; |
5974 |
SmallVector Worklist; |
| 5975 |
|
5975 |
|
| 5976 |
auto AddWork = [&](Value *V) { |
5976 |
auto AddWork = [&](Value *V) { |
| 5977 |
if (Visited.insert(V).second) |
5977 |
if (Visited.insert(V).second) |
| 5978 |
Worklist.push_back(V); |
5978 |
Worklist.push_back(V); |
| 5979 |
}; |
5979 |
}; |
| 5980 |
|
5980 |
|
| 5981 |
AddWork(V); |
5981 |
AddWork(V); |
| 5982 |
do { |
5982 |
do { |
| 5983 |
V = Worklist.pop_back_val(); |
5983 |
V = Worklist.pop_back_val(); |
| 5984 |
assert(Visited.count(V)); |
5984 |
assert(Visited.count(V)); |
| 5985 |
|
5985 |
|
| 5986 |
if (AllocaInst *AI = dyn_cast(V)) { |
5986 |
if (AllocaInst *AI = dyn_cast(V)) { |
| 5987 |
if (Result && Result != AI) |
5987 |
if (Result && Result != AI) |
| 5988 |
return nullptr; |
5988 |
return nullptr; |
| 5989 |
Result = AI; |
5989 |
Result = AI; |
| 5990 |
} else if (CastInst *CI = dyn_cast(V)) { |
5990 |
} else if (CastInst *CI = dyn_cast(V)) { |
| 5991 |
AddWork(CI->getOperand(0)); |
5991 |
AddWork(CI->getOperand(0)); |
| 5992 |
} else if (PHINode *PN = dyn_cast(V)) { |
5992 |
} else if (PHINode *PN = dyn_cast(V)) { |
| 5993 |
for (Value *IncValue : PN->incoming_values()) |
5993 |
for (Value *IncValue : PN->incoming_values()) |
| 5994 |
AddWork(IncValue); |
5994 |
AddWork(IncValue); |
| 5995 |
} else if (auto *SI = dyn_cast(V)) { |
5995 |
} else if (auto *SI = dyn_cast(V)) { |
| 5996 |
AddWork(SI->getTrueValue()); |
5996 |
AddWork(SI->getTrueValue()); |
| 5997 |
AddWork(SI->getFalseValue()); |
5997 |
AddWork(SI->getFalseValue()); |
| 5998 |
} else if (GetElementPtrInst *GEP = dyn_cast(V)) { |
5998 |
} else if (GetElementPtrInst *GEP = dyn_cast(V)) { |
| 5999 |
if (OffsetZero && !GEP->hasAllZeroIndices()) |
5999 |
if (OffsetZero && !GEP->hasAllZeroIndices()) |
| 6000 |
return nullptr; |
6000 |
return nullptr; |
| 6001 |
AddWork(GEP->getPointerOperand()); |
6001 |
AddWork(GEP->getPointerOperand()); |
| 6002 |
} else if (CallBase *CB = dyn_cast(V)) { |
6002 |
} else if (CallBase *CB = dyn_cast(V)) { |
| 6003 |
Value *Returned = CB->getReturnedArgOperand(); |
6003 |
Value *Returned = CB->getReturnedArgOperand(); |
| 6004 |
if (Returned) |
6004 |
if (Returned) |
| 6005 |
AddWork(Returned); |
6005 |
AddWork(Returned); |
| 6006 |
else |
6006 |
else |
| 6007 |
return nullptr; |
6007 |
return nullptr; |
| 6008 |
} else { |
6008 |
} else { |
| 6009 |
return nullptr; |
6009 |
return nullptr; |
| 6010 |
} |
6010 |
} |
| 6011 |
} while (!Worklist.empty()); |
6011 |
} while (!Worklist.empty()); |
| 6012 |
|
6012 |
|
| 6013 |
return Result; |
6013 |
return Result; |
| 6014 |
} |
6014 |
} |
| 6015 |
|
6015 |
|
| 6016 |
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper( |
6016 |
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper( |
| 6017 |
const Value *V, bool AllowLifetime, bool AllowDroppable) { |
6017 |
const Value *V, bool AllowLifetime, bool AllowDroppable) { |
| 6018 |
for (const User *U : V->users()) { |
6018 |
for (const User *U : V->users()) { |
| 6019 |
const IntrinsicInst *II = dyn_cast(U); |
6019 |
const IntrinsicInst *II = dyn_cast(U); |
| 6020 |
if (!II) |
6020 |
if (!II) |
| 6021 |
return false; |
6021 |
return false; |
| 6022 |
|
6022 |
|
| 6023 |
if (AllowLifetime && II->isLifetimeStartOrEnd()) |
6023 |
if (AllowLifetime && II->isLifetimeStartOrEnd()) |
| 6024 |
continue; |
6024 |
continue; |
| 6025 |
|
6025 |
|
| 6026 |
if (AllowDroppable && II->isDroppable()) |
6026 |
if (AllowDroppable && II->isDroppable()) |
| 6027 |
continue; |
6027 |
continue; |
| 6028 |
|
6028 |
|
| 6029 |
return false; |
6029 |
return false; |
| 6030 |
} |
6030 |
} |
| 6031 |
return true; |
6031 |
return true; |
| 6032 |
} |
6032 |
} |
| 6033 |
|
6033 |
|
| 6034 |
bool llvm::onlyUsedByLifetimeMarkers(const Value *V) { |
6034 |
bool llvm::onlyUsedByLifetimeMarkers(const Value *V) { |
| 6035 |
return onlyUsedByLifetimeMarkersOrDroppableInstsHelper( |
6035 |
return onlyUsedByLifetimeMarkersOrDroppableInstsHelper( |
| 6036 |
V, /* AllowLifetime */ true, /* AllowDroppable */ false); |
6036 |
V, /* AllowLifetime */ true, /* AllowDroppable */ false); |
| 6037 |
} |
6037 |
} |
| 6038 |
bool llvm::onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V) { |
6038 |
bool llvm::onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V) { |
| 6039 |
return onlyUsedByLifetimeMarkersOrDroppableInstsHelper( |
6039 |
return onlyUsedByLifetimeMarkersOrDroppableInstsHelper( |
| 6040 |
V, /* AllowLifetime */ true, /* AllowDroppable */ true); |
6040 |
V, /* AllowLifetime */ true, /* AllowDroppable */ true); |
| 6041 |
} |
6041 |
} |
| 6042 |
|
6042 |
|
| 6043 |
bool llvm::mustSuppressSpeculation(const LoadInst &LI) { |
6043 |
bool llvm::mustSuppressSpeculation(const LoadInst &LI) { |
| 6044 |
if (!LI.isUnordered()) |
6044 |
if (!LI.isUnordered()) |
| 6045 |
return true; |
6045 |
return true; |
| 6046 |
const Function &F = *LI.getFunction(); |
6046 |
const Function &F = *LI.getFunction(); |
| 6047 |
// Speculative load may create a race that did not exist in the source. |
6047 |
// Speculative load may create a race that did not exist in the source. |
| 6048 |
return F.hasFnAttribute(Attribute::SanitizeThread) || |
6048 |
return F.hasFnAttribute(Attribute::SanitizeThread) || |
| 6049 |
// Speculative load may load data from dirty regions. |
6049 |
// Speculative load may load data from dirty regions. |
| 6050 |
F.hasFnAttribute(Attribute::SanitizeAddress) || |
6050 |
F.hasFnAttribute(Attribute::SanitizeAddress) || |
| 6051 |
F.hasFnAttribute(Attribute::SanitizeHWAddress); |
6051 |
F.hasFnAttribute(Attribute::SanitizeHWAddress); |
| 6052 |
} |
6052 |
} |
| 6053 |
|
6053 |
|
| 6054 |
bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst, |
6054 |
bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst, |
| 6055 |
const Instruction *CtxI, |
6055 |
const Instruction *CtxI, |
| 6056 |
AssumptionCache *AC, |
6056 |
AssumptionCache *AC, |
| 6057 |
const DominatorTree *DT, |
6057 |
const DominatorTree *DT, |
| 6058 |
const TargetLibraryInfo *TLI) { |
6058 |
const TargetLibraryInfo *TLI) { |
| 6059 |
return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI, |
6059 |
return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI, |
| 6060 |
AC, DT, TLI); |
6060 |
AC, DT, TLI); |
| 6061 |
} |
6061 |
} |
| 6062 |
|
6062 |
|
| 6063 |
bool llvm::isSafeToSpeculativelyExecuteWithOpcode( |
6063 |
bool llvm::isSafeToSpeculativelyExecuteWithOpcode( |
| 6064 |
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI, |
6064 |
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI, |
| 6065 |
AssumptionCache *AC, const DominatorTree *DT, |
6065 |
AssumptionCache *AC, const DominatorTree *DT, |
| 6066 |
const TargetLibraryInfo *TLI) { |
6066 |
const TargetLibraryInfo *TLI) { |
| 6067 |
#ifndef NDEBUG |
6067 |
#ifndef NDEBUG |
| 6068 |
if (Inst->getOpcode() != Opcode) { |
6068 |
if (Inst->getOpcode() != Opcode) { |
| 6069 |
// Check that the operands are actually compatible with the Opcode override. |
6069 |
// Check that the operands are actually compatible with the Opcode override. |
| 6070 |
auto hasEqualReturnAndLeadingOperandTypes = |
6070 |
auto hasEqualReturnAndLeadingOperandTypes = |
| 6071 |
[](const Instruction *Inst, unsigned NumLeadingOperands) { |
6071 |
[](const Instruction *Inst, unsigned NumLeadingOperands) { |
| 6072 |
if (Inst->getNumOperands() < NumLeadingOperands) |
6072 |
if (Inst->getNumOperands() < NumLeadingOperands) |
| 6073 |
return false; |
6073 |
return false; |
| 6074 |
const Type *ExpectedType = Inst->getType(); |
6074 |
const Type *ExpectedType = Inst->getType(); |
| 6075 |
for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp) |
6075 |
for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp) |
| 6076 |
if (Inst->getOperand(ItOp)->getType() != ExpectedType) |
6076 |
if (Inst->getOperand(ItOp)->getType() != ExpectedType) |
| 6077 |
return false; |
6077 |
return false; |
| 6078 |
return true; |
6078 |
return true; |
| 6079 |
}; |
6079 |
}; |
| 6080 |
assert(!Instruction::isBinaryOp(Opcode) || |
6080 |
assert(!Instruction::isBinaryOp(Opcode) || |
| 6081 |
hasEqualReturnAndLeadingOperandTypes(Inst, 2)); |
6081 |
hasEqualReturnAndLeadingOperandTypes(Inst, 2)); |
| 6082 |
assert(!Instruction::isUnaryOp(Opcode) || |
6082 |
assert(!Instruction::isUnaryOp(Opcode) || |
| 6083 |
hasEqualReturnAndLeadingOperandTypes(Inst, 1)); |
6083 |
hasEqualReturnAndLeadingOperandTypes(Inst, 1)); |
| 6084 |
} |
6084 |
} |
| 6085 |
#endif |
6085 |
#endif |
| 6086 |
|
6086 |
|
| 6087 |
switch (Opcode) { |
6087 |
switch (Opcode) { |
| 6088 |
default: |
6088 |
default: |
| 6089 |
return true; |
6089 |
return true; |
| 6090 |
case Instruction::UDiv: |
6090 |
case Instruction::UDiv: |
| 6091 |
case Instruction::URem: { |
6091 |
case Instruction::URem: { |
| 6092 |
// x / y is undefined if y == 0. |
6092 |
// x / y is undefined if y == 0. |
| 6093 |
const APInt *V; |
6093 |
const APInt *V; |
| 6094 |
if (match(Inst->getOperand(1), m_APInt(V))) |
6094 |
if (match(Inst->getOperand(1), m_APInt(V))) |
| 6095 |
return *V != 0; |
6095 |
return *V != 0; |
| 6096 |
return false; |
6096 |
return false; |
| 6097 |
} |
6097 |
} |
| 6098 |
case Instruction::SDiv: |
6098 |
case Instruction::SDiv: |
| 6099 |
case Instruction::SRem: { |
6099 |
case Instruction::SRem: { |
| 6100 |
// x / y is undefined if y == 0 or x == INT_MIN and y == -1 |
6100 |
// x / y is undefined if y == 0 or x == INT_MIN and y == -1 |
| 6101 |
const APInt *Numerator, *Denominator; |
6101 |
const APInt *Numerator, *Denominator; |
| 6102 |
if (!match(Inst->getOperand(1), m_APInt(Denominator))) |
6102 |
if (!match(Inst->getOperand(1), m_APInt(Denominator))) |
| 6103 |
return false; |
6103 |
return false; |
| 6104 |
// We cannot hoist this division if the denominator is 0. |
6104 |
// We cannot hoist this division if the denominator is 0. |
| 6105 |
if (*Denominator == 0) |
6105 |
if (*Denominator == 0) |
| 6106 |
return false; |
6106 |
return false; |
| 6107 |
// It's safe to hoist if the denominator is not 0 or -1. |
6107 |
// It's safe to hoist if the denominator is not 0 or -1. |
| 6108 |
if (!Denominator->isAllOnes()) |
6108 |
if (!Denominator->isAllOnes()) |
| 6109 |
return true; |
6109 |
return true; |
| 6110 |
// At this point we know that the denominator is -1. It is safe to hoist as |
6110 |
// At this point we know that the denominator is -1. It is safe to hoist as |
| 6111 |
// long we know that the numerator is not INT_MIN. |
6111 |
// long we know that the numerator is not INT_MIN. |
| 6112 |
if (match(Inst->getOperand(0), m_APInt(Numerator))) |
6112 |
if (match(Inst->getOperand(0), m_APInt(Numerator))) |
| 6113 |
return !Numerator->isMinSignedValue(); |
6113 |
return !Numerator->isMinSignedValue(); |
| 6114 |
// The numerator *might* be MinSignedValue. |
6114 |
// The numerator *might* be MinSignedValue. |
| 6115 |
return false; |
6115 |
return false; |
| 6116 |
} |
6116 |
} |
| 6117 |
case Instruction::Load: { |
6117 |
case Instruction::Load: { |
| 6118 |
const LoadInst *LI = dyn_cast(Inst); |
6118 |
const LoadInst *LI = dyn_cast(Inst); |
| 6119 |
if (!LI) |
6119 |
if (!LI) |
| 6120 |
return false; |
6120 |
return false; |
| 6121 |
if (mustSuppressSpeculation(*LI)) |
6121 |
if (mustSuppressSpeculation(*LI)) |
| 6122 |
return false; |
6122 |
return false; |
| 6123 |
const DataLayout &DL = LI->getModule()->getDataLayout(); |
6123 |
const DataLayout &DL = LI->getModule()->getDataLayout(); |
| 6124 |
return isDereferenceableAndAlignedPointer(LI->getPointerOperand(), |
6124 |
return isDereferenceableAndAlignedPointer(LI->getPointerOperand(), |
| 6125 |
LI->getType(), LI->getAlign(), DL, |
6125 |
LI->getType(), LI->getAlign(), DL, |
| 6126 |
CtxI, AC, DT, TLI); |
6126 |
CtxI, AC, DT, TLI); |
| 6127 |
} |
6127 |
} |
| 6128 |
case Instruction::Call: { |
6128 |
case Instruction::Call: { |
| 6129 |
auto *CI = dyn_cast(Inst); |
6129 |
auto *CI = dyn_cast(Inst); |
| 6130 |
if (!CI) |
6130 |
if (!CI) |
| 6131 |
return false; |
6131 |
return false; |
| 6132 |
const Function *Callee = CI->getCalledFunction(); |
6132 |
const Function *Callee = CI->getCalledFunction(); |
| 6133 |
|
6133 |
|
| 6134 |
// The called function could have undefined behavior or side-effects, even |
6134 |
// The called function could have undefined behavior or side-effects, even |
| 6135 |
// if marked readnone nounwind. |
6135 |
// if marked readnone nounwind. |
| 6136 |
return Callee && Callee->isSpeculatable(); |
6136 |
return Callee && Callee->isSpeculatable(); |
| 6137 |
} |
6137 |
} |
| 6138 |
case Instruction::VAArg: |
6138 |
case Instruction::VAArg: |
| 6139 |
case Instruction::Alloca: |
6139 |
case Instruction::Alloca: |
| 6140 |
case Instruction::Invoke: |
6140 |
case Instruction::Invoke: |
| 6141 |
case Instruction::CallBr: |
6141 |
case Instruction::CallBr: |
| 6142 |
case Instruction::PHI: |
6142 |
case Instruction::PHI: |
| 6143 |
case Instruction::Store: |
6143 |
case Instruction::Store: |
| 6144 |
case Instruction::Ret: |
6144 |
case Instruction::Ret: |
| 6145 |
case Instruction::Br: |
6145 |
case Instruction::Br: |
| 6146 |
case Instruction::IndirectBr: |
6146 |
case Instruction::IndirectBr: |
| 6147 |
case Instruction::Switch: |
6147 |
case Instruction::Switch: |
| 6148 |
case Instruction::Unreachable: |
6148 |
case Instruction::Unreachable: |
| 6149 |
case Instruction::Fence: |
6149 |
case Instruction::Fence: |
| 6150 |
case Instruction::AtomicRMW: |
6150 |
case Instruction::AtomicRMW: |
| 6151 |
case Instruction::AtomicCmpXchg: |
6151 |
case Instruction::AtomicCmpXchg: |
| 6152 |
case Instruction::LandingPad: |
6152 |
case Instruction::LandingPad: |
| 6153 |
case Instruction::Resume: |
6153 |
case Instruction::Resume: |
| 6154 |
case Instruction::CatchSwitch: |
6154 |
case Instruction::CatchSwitch: |
| 6155 |
case Instruction::CatchPad: |
6155 |
case Instruction::CatchPad: |
| 6156 |
case Instruction::CatchRet: |
6156 |
case Instruction::CatchRet: |
| 6157 |
case Instruction::CleanupPad: |
6157 |
case Instruction::CleanupPad: |
| 6158 |
case Instruction::CleanupRet: |
6158 |
case Instruction::CleanupRet: |
| 6159 |
return false; // Misc instructions which have effects |
6159 |
return false; // Misc instructions which have effects |
| 6160 |
} |
6160 |
} |
| 6161 |
} |
6161 |
} |
| 6162 |
|
6162 |
|
| 6163 |
bool llvm::mayHaveNonDefUseDependency(const Instruction &I) { |
6163 |
bool llvm::mayHaveNonDefUseDependency(const Instruction &I) { |
| 6164 |
if (I.mayReadOrWriteMemory()) |
6164 |
if (I.mayReadOrWriteMemory()) |
| 6165 |
// Memory dependency possible |
6165 |
// Memory dependency possible |
| 6166 |
return true; |
6166 |
return true; |
| 6167 |
if (!isSafeToSpeculativelyExecute(&I)) |
6167 |
if (!isSafeToSpeculativelyExecute(&I)) |
| 6168 |
// Can't move above a maythrow call or infinite loop. Or if an |
6168 |
// Can't move above a maythrow call or infinite loop. Or if an |
| 6169 |
// inalloca alloca, above a stacksave call. |
6169 |
// inalloca alloca, above a stacksave call. |
| 6170 |
return true; |
6170 |
return true; |
| 6171 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
6171 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
| 6172 |
// 1) Can't reorder two inf-loop calls, even if readonly |
6172 |
// 1) Can't reorder two inf-loop calls, even if readonly |
| 6173 |
// 2) Also can't reorder an inf-loop call below a instruction which isn't |
6173 |
// 2) Also can't reorder an inf-loop call below a instruction which isn't |
| 6174 |
// safe to speculative execute. (Inverse of above) |
6174 |
// safe to speculative execute. (Inverse of above) |
| 6175 |
return true; |
6175 |
return true; |
| 6176 |
return false; |
6176 |
return false; |
| 6177 |
} |
6177 |
} |
| 6178 |
|
6178 |
|
| 6179 |
/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult. |
6179 |
/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult. |
| 6180 |
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) { |
6180 |
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) { |
| 6181 |
switch (OR) { |
6181 |
switch (OR) { |
| 6182 |
case ConstantRange::OverflowResult::MayOverflow: |
6182 |
case ConstantRange::OverflowResult::MayOverflow: |
| 6183 |
return OverflowResult::MayOverflow; |
6183 |
return OverflowResult::MayOverflow; |
| 6184 |
case ConstantRange::OverflowResult::AlwaysOverflowsLow: |
6184 |
case ConstantRange::OverflowResult::AlwaysOverflowsLow: |
| 6185 |
return OverflowResult::AlwaysOverflowsLow; |
6185 |
return OverflowResult::AlwaysOverflowsLow; |
| 6186 |
case ConstantRange::OverflowResult::AlwaysOverflowsHigh: |
6186 |
case ConstantRange::OverflowResult::AlwaysOverflowsHigh: |
| 6187 |
return OverflowResult::AlwaysOverflowsHigh; |
6187 |
return OverflowResult::AlwaysOverflowsHigh; |
| 6188 |
case ConstantRange::OverflowResult::NeverOverflows: |
6188 |
case ConstantRange::OverflowResult::NeverOverflows: |
| 6189 |
return OverflowResult::NeverOverflows; |
6189 |
return OverflowResult::NeverOverflows; |
| 6190 |
} |
6190 |
} |
| 6191 |
llvm_unreachable("Unknown OverflowResult"); |
6191 |
llvm_unreachable("Unknown OverflowResult"); |
| 6192 |
} |
6192 |
} |
| 6193 |
|
6193 |
|
| 6194 |
/// Combine constant ranges from computeConstantRange() and computeKnownBits(). |
6194 |
/// Combine constant ranges from computeConstantRange() and computeKnownBits(). |
| 6195 |
static ConstantRange computeConstantRangeIncludingKnownBits( |
6195 |
static ConstantRange computeConstantRangeIncludingKnownBits( |
| 6196 |
const Value *V, bool ForSigned, const DataLayout &DL, unsigned Depth, |
6196 |
const Value *V, bool ForSigned, const DataLayout &DL, unsigned Depth, |
| 6197 |
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
6197 |
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
| 6198 |
bool UseInstrInfo = true) { |
6198 |
bool UseInstrInfo = true) { |
| 6199 |
KnownBits Known = computeKnownBits(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
6199 |
KnownBits Known = computeKnownBits(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
| 6200 |
ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned); |
6200 |
ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned); |
| 6201 |
ConstantRange CR2 = computeConstantRange(V, ForSigned, UseInstrInfo); |
6201 |
ConstantRange CR2 = computeConstantRange(V, ForSigned, UseInstrInfo); |
| 6202 |
ConstantRange::PreferredRangeType RangeType = |
6202 |
ConstantRange::PreferredRangeType RangeType = |
| 6203 |
ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned; |
6203 |
ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned; |
| 6204 |
return CR1.intersectWith(CR2, RangeType); |
6204 |
return CR1.intersectWith(CR2, RangeType); |
| 6205 |
} |
6205 |
} |
| 6206 |
|
6206 |
|
| 6207 |
OverflowResult llvm::computeOverflowForUnsignedMul( |
6207 |
OverflowResult llvm::computeOverflowForUnsignedMul( |
| 6208 |
const Value *LHS, const Value *RHS, const DataLayout &DL, |
6208 |
const Value *LHS, const Value *RHS, const DataLayout &DL, |
| 6209 |
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
6209 |
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
| 6210 |
bool UseInstrInfo) { |
6210 |
bool UseInstrInfo) { |
| 6211 |
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, |
6211 |
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 6212 |
UseInstrInfo); |
6212 |
UseInstrInfo); |
| 6213 |
KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, |
6213 |
KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 6214 |
UseInstrInfo); |
6214 |
UseInstrInfo); |
| 6215 |
ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false); |
6215 |
ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false); |
| 6216 |
ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false); |
6216 |
ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false); |
| 6217 |
return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange)); |
6217 |
return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange)); |
| 6218 |
} |
6218 |
} |
| 6219 |
|
6219 |
|
| 6220 |
OverflowResult |
6220 |
OverflowResult |
| 6221 |
llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS, |
6221 |
llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS, |
| 6222 |
const DataLayout &DL, AssumptionCache *AC, |
6222 |
const DataLayout &DL, AssumptionCache *AC, |
| 6223 |
const Instruction *CxtI, |
6223 |
const Instruction *CxtI, |
| 6224 |
const DominatorTree *DT, bool UseInstrInfo) { |
6224 |
const DominatorTree *DT, bool UseInstrInfo) { |
| 6225 |
// Multiplying n * m significant bits yields a result of n + m significant |
6225 |
// Multiplying n * m significant bits yields a result of n + m significant |
| 6226 |
// bits. If the total number of significant bits does not exceed the |
6226 |
// bits. If the total number of significant bits does not exceed the |
| 6227 |
// result bit width (minus 1), there is no overflow. |
6227 |
// result bit width (minus 1), there is no overflow. |
| 6228 |
// This means if we have enough leading sign bits in the operands |
6228 |
// This means if we have enough leading sign bits in the operands |
| 6229 |
// we can guarantee that the result does not overflow. |
6229 |
// we can guarantee that the result does not overflow. |
| 6230 |
// Ref: "Hacker's Delight" by Henry Warren |
6230 |
// Ref: "Hacker's Delight" by Henry Warren |
| 6231 |
unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
6231 |
unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
| 6232 |
|
6232 |
|
| 6233 |
// Note that underestimating the number of sign bits gives a more |
6233 |
// Note that underestimating the number of sign bits gives a more |
| 6234 |
// conservative answer. |
6234 |
// conservative answer. |
| 6235 |
unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) + |
6235 |
unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) + |
| 6236 |
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT); |
6236 |
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT); |
| 6237 |
|
6237 |
|
| 6238 |
// First handle the easy case: if we have enough sign bits there's |
6238 |
// First handle the easy case: if we have enough sign bits there's |
| 6239 |
// definitely no overflow. |
6239 |
// definitely no overflow. |
| 6240 |
if (SignBits > BitWidth + 1) |
6240 |
if (SignBits > BitWidth + 1) |
| 6241 |
return OverflowResult::NeverOverflows; |
6241 |
return OverflowResult::NeverOverflows; |
| 6242 |
|
6242 |
|
| 6243 |
// There are two ambiguous cases where there can be no overflow: |
6243 |
// There are two ambiguous cases where there can be no overflow: |
| 6244 |
// SignBits == BitWidth + 1 and |
6244 |
// SignBits == BitWidth + 1 and |
| 6245 |
// SignBits == BitWidth |
6245 |
// SignBits == BitWidth |
| 6246 |
// The second case is difficult to check, therefore we only handle the |
6246 |
// The second case is difficult to check, therefore we only handle the |
| 6247 |
// first case. |
6247 |
// first case. |
| 6248 |
if (SignBits == BitWidth + 1) { |
6248 |
if (SignBits == BitWidth + 1) { |
| 6249 |
// It overflows only when both arguments are negative and the true |
6249 |
// It overflows only when both arguments are negative and the true |
| 6250 |
// product is exactly the minimum negative number. |
6250 |
// product is exactly the minimum negative number. |
| 6251 |
// E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 |
6251 |
// E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 |
| 6252 |
// For simplicity we just check if at least one side is not negative. |
6252 |
// For simplicity we just check if at least one side is not negative. |
| 6253 |
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, |
6253 |
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 6254 |
UseInstrInfo); |
6254 |
UseInstrInfo); |
| 6255 |
KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, |
6255 |
KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 6256 |
UseInstrInfo); |
6256 |
UseInstrInfo); |
| 6257 |
if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) |
6257 |
if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) |
| 6258 |
return OverflowResult::NeverOverflows; |
6258 |
return OverflowResult::NeverOverflows; |
| 6259 |
} |
6259 |
} |
| 6260 |
return OverflowResult::MayOverflow; |
6260 |
return OverflowResult::MayOverflow; |
| 6261 |
} |
6261 |
} |
| 6262 |
|
6262 |
|
| 6263 |
OverflowResult llvm::computeOverflowForUnsignedAdd( |
6263 |
OverflowResult llvm::computeOverflowForUnsignedAdd( |
| 6264 |
const Value *LHS, const Value *RHS, const DataLayout &DL, |
6264 |
const Value *LHS, const Value *RHS, const DataLayout &DL, |
| 6265 |
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
6265 |
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
| 6266 |
bool UseInstrInfo) { |
6266 |
bool UseInstrInfo) { |
| 6267 |
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( |
6267 |
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( |
| 6268 |
LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT, UseInstrInfo); |
6268 |
LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT, UseInstrInfo); |
| 6269 |
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( |
6269 |
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( |
| 6270 |
RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT, UseInstrInfo); |
6270 |
RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT, UseInstrInfo); |
| 6271 |
return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange)); |
6271 |
return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange)); |
| 6272 |
} |
6272 |
} |
| 6273 |
|
6273 |
|
| 6274 |
static OverflowResult computeOverflowForSignedAdd(const Value *LHS, |
6274 |
static OverflowResult computeOverflowForSignedAdd(const Value *LHS, |
| 6275 |
const Value *RHS, |
6275 |
const Value *RHS, |
| 6276 |
const AddOperator *Add, |
6276 |
const AddOperator *Add, |
| 6277 |
const DataLayout &DL, |
6277 |
const DataLayout &DL, |
| 6278 |
AssumptionCache *AC, |
6278 |
AssumptionCache *AC, |
| 6279 |
const Instruction *CxtI, |
6279 |
const Instruction *CxtI, |
| 6280 |
const DominatorTree *DT) { |
6280 |
const DominatorTree *DT) { |
| 6281 |
if (Add && Add->hasNoSignedWrap()) { |
6281 |
if (Add && Add->hasNoSignedWrap()) { |
| 6282 |
return OverflowResult::NeverOverflows; |
6282 |
return OverflowResult::NeverOverflows; |
| 6283 |
} |
6283 |
} |
| 6284 |
|
6284 |
|
| 6285 |
// If LHS and RHS each have at least two sign bits, the addition will look |
6285 |
// If LHS and RHS each have at least two sign bits, the addition will look |
| 6286 |
// like |
6286 |
// like |
| 6287 |
// |
6287 |
// |
| 6288 |
// XX..... + |
6288 |
// XX..... + |
| 6289 |
// YY..... |
6289 |
// YY..... |
| 6290 |
// |
6290 |
// |
| 6291 |
// If the carry into the most significant position is 0, X and Y can't both |
6291 |
// If the carry into the most significant position is 0, X and Y can't both |
| 6292 |
// be 1 and therefore the carry out of the addition is also 0. |
6292 |
// be 1 and therefore the carry out of the addition is also 0. |
| 6293 |
// |
6293 |
// |
| 6294 |
// If the carry into the most significant position is 1, X and Y can't both |
6294 |
// If the carry into the most significant position is 1, X and Y can't both |
| 6295 |
// be 0 and therefore the carry out of the addition is also 1. |
6295 |
// be 0 and therefore the carry out of the addition is also 1. |
| 6296 |
// |
6296 |
// |
| 6297 |
// Since the carry into the most significant position is always equal to |
6297 |
// Since the carry into the most significant position is always equal to |
| 6298 |
// the carry out of the addition, there is no signed overflow. |
6298 |
// the carry out of the addition, there is no signed overflow. |
| 6299 |
if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && |
6299 |
if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && |
| 6300 |
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) |
6300 |
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) |
| 6301 |
return OverflowResult::NeverOverflows; |
6301 |
return OverflowResult::NeverOverflows; |
| 6302 |
|
6302 |
|
| 6303 |
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( |
6303 |
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( |
| 6304 |
LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); |
6304 |
LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); |
| 6305 |
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( |
6305 |
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( |
| 6306 |
RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); |
6306 |
RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); |
| 6307 |
OverflowResult OR = |
6307 |
OverflowResult OR = |
| 6308 |
mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange)); |
6308 |
mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange)); |
| 6309 |
if (OR != OverflowResult::MayOverflow) |
6309 |
if (OR != OverflowResult::MayOverflow) |
| 6310 |
return OR; |
6310 |
return OR; |
| 6311 |
|
6311 |
|
| 6312 |
// The remaining code needs Add to be available. Early returns if not so. |
6312 |
// The remaining code needs Add to be available. Early returns if not so. |
| 6313 |
if (!Add) |
6313 |
if (!Add) |
| 6314 |
return OverflowResult::MayOverflow; |
6314 |
return OverflowResult::MayOverflow; |
| 6315 |
|
6315 |
|
| 6316 |
// If the sign of Add is the same as at least one of the operands, this add |
6316 |
// If the sign of Add is the same as at least one of the operands, this add |
| 6317 |
// CANNOT overflow. If this can be determined from the known bits of the |
6317 |
// CANNOT overflow. If this can be determined from the known bits of the |
| 6318 |
// operands the above signedAddMayOverflow() check will have already done so. |
6318 |
// operands the above signedAddMayOverflow() check will have already done so. |
| 6319 |
// The only other way to improve on the known bits is from an assumption, so |
6319 |
// The only other way to improve on the known bits is from an assumption, so |
| 6320 |
// call computeKnownBitsFromAssume() directly. |
6320 |
// call computeKnownBitsFromAssume() directly. |
| 6321 |
bool LHSOrRHSKnownNonNegative = |
6321 |
bool LHSOrRHSKnownNonNegative = |
| 6322 |
(LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative()); |
6322 |
(LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative()); |
| 6323 |
bool LHSOrRHSKnownNegative = |
6323 |
bool LHSOrRHSKnownNegative = |
| 6324 |
(LHSRange.isAllNegative() || RHSRange.isAllNegative()); |
6324 |
(LHSRange.isAllNegative() || RHSRange.isAllNegative()); |
| 6325 |
if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) { |
6325 |
if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) { |
| 6326 |
KnownBits AddKnown(LHSRange.getBitWidth()); |
6326 |
KnownBits AddKnown(LHSRange.getBitWidth()); |
| 6327 |
computeKnownBitsFromAssume( |
6327 |
computeKnownBitsFromAssume( |
| 6328 |
Add, AddKnown, /*Depth=*/0, |
6328 |
Add, AddKnown, /*Depth=*/0, |
| 6329 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, CxtI, DT)); |
6329 |
SimplifyQuery(DL, /*TLI*/ nullptr, DT, AC, CxtI, DT)); |
| 6330 |
if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) || |
6330 |
if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) || |
| 6331 |
(AddKnown.isNegative() && LHSOrRHSKnownNegative)) |
6331 |
(AddKnown.isNegative() && LHSOrRHSKnownNegative)) |
| 6332 |
return OverflowResult::NeverOverflows; |
6332 |
return OverflowResult::NeverOverflows; |
| 6333 |
} |
6333 |
} |
| 6334 |
|
6334 |
|
| 6335 |
return OverflowResult::MayOverflow; |
6335 |
return OverflowResult::MayOverflow; |
| 6336 |
} |
6336 |
} |
| 6337 |
|
6337 |
|
| 6338 |
OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS, |
6338 |
OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS, |
| 6339 |
const Value *RHS, |
6339 |
const Value *RHS, |
| 6340 |
const DataLayout &DL, |
6340 |
const DataLayout &DL, |
| 6341 |
AssumptionCache *AC, |
6341 |
AssumptionCache *AC, |
| 6342 |
const Instruction *CxtI, |
6342 |
const Instruction *CxtI, |
| 6343 |
const DominatorTree *DT) { |
6343 |
const DominatorTree *DT) { |
| 6344 |
// X - (X % ?) |
6344 |
// X - (X % ?) |
| 6345 |
// The remainder of a value can't have greater magnitude than itself, |
6345 |
// The remainder of a value can't have greater magnitude than itself, |
| 6346 |
// so the subtraction can't overflow. |
6346 |
// so the subtraction can't overflow. |
| 6347 |
|
6347 |
|
| 6348 |
// X - (X -nuw ?) |
6348 |
// X - (X -nuw ?) |
| 6349 |
// In the minimal case, this would simplify to "?", so there's no subtract |
6349 |
// In the minimal case, this would simplify to "?", so there's no subtract |
| 6350 |
// at all. But if this analysis is used to peek through casts, for example, |
6350 |
// at all. But if this analysis is used to peek through casts, for example, |
| 6351 |
// then determining no-overflow may allow other transforms. |
6351 |
// then determining no-overflow may allow other transforms. |
| 6352 |
|
6352 |
|
| 6353 |
// TODO: There are other patterns like this. |
6353 |
// TODO: There are other patterns like this. |
| 6354 |
// See simplifyICmpWithBinOpOnLHS() for candidates. |
6354 |
// See simplifyICmpWithBinOpOnLHS() for candidates. |
| 6355 |
if (match(RHS, m_URem(m_Specific(LHS), m_Value())) || |
6355 |
if (match(RHS, m_URem(m_Specific(LHS), m_Value())) || |
| 6356 |
match(RHS, m_NUWSub(m_Specific(LHS), m_Value()))) |
6356 |
match(RHS, m_NUWSub(m_Specific(LHS), m_Value()))) |
| 6357 |
if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT)) |
6357 |
if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT)) |
| 6358 |
return OverflowResult::NeverOverflows; |
6358 |
return OverflowResult::NeverOverflows; |
| 6359 |
|
6359 |
|
| 6360 |
// Checking for conditions implied by dominating conditions may be expensive. |
6360 |
// Checking for conditions implied by dominating conditions may be expensive. |
| 6361 |
// Limit it to usub_with_overflow calls for now. |
6361 |
// Limit it to usub_with_overflow calls for now. |
| 6362 |
if (match(CxtI, |
6362 |
if (match(CxtI, |
| 6363 |
m_Intrinsic(m_Value(), m_Value()))) |
6363 |
m_Intrinsic(m_Value(), m_Value()))) |
| 6364 |
if (auto C = |
6364 |
if (auto C = |
| 6365 |
isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, CxtI, DL)) { |
6365 |
isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, CxtI, DL)) { |
| 6366 |
if (*C) |
6366 |
if (*C) |
| 6367 |
return OverflowResult::NeverOverflows; |
6367 |
return OverflowResult::NeverOverflows; |
| 6368 |
return OverflowResult::AlwaysOverflowsLow; |
6368 |
return OverflowResult::AlwaysOverflowsLow; |
| 6369 |
} |
6369 |
} |
| 6370 |
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( |
6370 |
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( |
| 6371 |
LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT); |
6371 |
LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT); |
| 6372 |
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( |
6372 |
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( |
| 6373 |
RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT); |
6373 |
RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT); |
| 6374 |
return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange)); |
6374 |
return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange)); |
| 6375 |
} |
6375 |
} |
| 6376 |
|
6376 |
|
| 6377 |
OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS, |
6377 |
OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS, |
| 6378 |
const Value *RHS, |
6378 |
const Value *RHS, |
| 6379 |
const DataLayout &DL, |
6379 |
const DataLayout &DL, |
| 6380 |
AssumptionCache *AC, |
6380 |
AssumptionCache *AC, |
| 6381 |
const Instruction *CxtI, |
6381 |
const Instruction *CxtI, |
| 6382 |
const DominatorTree *DT) { |
6382 |
const DominatorTree *DT) { |
| 6383 |
// X - (X % ?) |
6383 |
// X - (X % ?) |
| 6384 |
// The remainder of a value can't have greater magnitude than itself, |
6384 |
// The remainder of a value can't have greater magnitude than itself, |
| 6385 |
// so the subtraction can't overflow. |
6385 |
// so the subtraction can't overflow. |
| 6386 |
|
6386 |
|
| 6387 |
// X - (X -nsw ?) |
6387 |
// X - (X -nsw ?) |
| 6388 |
// In the minimal case, this would simplify to "?", so there's no subtract |
6388 |
// In the minimal case, this would simplify to "?", so there's no subtract |
| 6389 |
// at all. But if this analysis is used to peek through casts, for example, |
6389 |
// at all. But if this analysis is used to peek through casts, for example, |
| 6390 |
// then determining no-overflow may allow other transforms. |
6390 |
// then determining no-overflow may allow other transforms. |
| 6391 |
if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) || |
6391 |
if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) || |
| 6392 |
match(RHS, m_NSWSub(m_Specific(LHS), m_Value()))) |
6392 |
match(RHS, m_NSWSub(m_Specific(LHS), m_Value()))) |
| 6393 |
if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT)) |
6393 |
if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT)) |
| 6394 |
return OverflowResult::NeverOverflows; |
6394 |
return OverflowResult::NeverOverflows; |
| 6395 |
|
6395 |
|
| 6396 |
// If LHS and RHS each have at least two sign bits, the subtraction |
6396 |
// If LHS and RHS each have at least two sign bits, the subtraction |
| 6397 |
// cannot overflow. |
6397 |
// cannot overflow. |
| 6398 |
if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && |
6398 |
if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && |
| 6399 |
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) |
6399 |
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) |
| 6400 |
return OverflowResult::NeverOverflows; |
6400 |
return OverflowResult::NeverOverflows; |
| 6401 |
|
6401 |
|
| 6402 |
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( |
6402 |
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits( |
| 6403 |
LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); |
6403 |
LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); |
| 6404 |
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( |
6404 |
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits( |
| 6405 |
RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); |
6405 |
RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT); |
| 6406 |
return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange)); |
6406 |
return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange)); |
| 6407 |
} |
6407 |
} |
| 6408 |
|
6408 |
|
| 6409 |
bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, |
6409 |
bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, |
| 6410 |
const DominatorTree &DT) { |
6410 |
const DominatorTree &DT) { |
| 6411 |
SmallVector GuardingBranches; |
6411 |
SmallVector GuardingBranches; |
| 6412 |
SmallVector Results; |
6412 |
SmallVector Results; |
| 6413 |
|
6413 |
|
| 6414 |
for (const User *U : WO->users()) { |
6414 |
for (const User *U : WO->users()) { |
| 6415 |
if (const auto *EVI = dyn_cast(U)) { |
6415 |
if (const auto *EVI = dyn_cast(U)) { |
| 6416 |
assert(EVI->getNumIndices() == 1 && "Obvious from CI's type"); |
6416 |
assert(EVI->getNumIndices() == 1 && "Obvious from CI's type"); |
| 6417 |
|
6417 |
|
| 6418 |
if (EVI->getIndices()[0] == 0) |
6418 |
if (EVI->getIndices()[0] == 0) |
| 6419 |
Results.push_back(EVI); |
6419 |
Results.push_back(EVI); |
| 6420 |
else { |
6420 |
else { |
| 6421 |
assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type"); |
6421 |
assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type"); |
| 6422 |
|
6422 |
|
| 6423 |
for (const auto *U : EVI->users()) |
6423 |
for (const auto *U : EVI->users()) |
| 6424 |
if (const auto *B = dyn_cast(U)) { |
6424 |
if (const auto *B = dyn_cast(U)) { |
| 6425 |
assert(B->isConditional() && "How else is it using an i1?"); |
6425 |
assert(B->isConditional() && "How else is it using an i1?"); |
| 6426 |
GuardingBranches.push_back(B); |
6426 |
GuardingBranches.push_back(B); |
| 6427 |
} |
6427 |
} |
| 6428 |
} |
6428 |
} |
| 6429 |
} else { |
6429 |
} else { |
| 6430 |
// We are using the aggregate directly in a way we don't want to analyze |
6430 |
// We are using the aggregate directly in a way we don't want to analyze |
| 6431 |
// here (storing it to a global, say). |
6431 |
// here (storing it to a global, say). |
| 6432 |
return false; |
6432 |
return false; |
| 6433 |
} |
6433 |
} |
| 6434 |
} |
6434 |
} |
| 6435 |
|
6435 |
|
| 6436 |
auto AllUsesGuardedByBranch = [&](const BranchInst *BI) { |
6436 |
auto AllUsesGuardedByBranch = [&](const BranchInst *BI) { |
| 6437 |
BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1)); |
6437 |
BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1)); |
| 6438 |
if (!NoWrapEdge.isSingleEdge()) |
6438 |
if (!NoWrapEdge.isSingleEdge()) |
| 6439 |
return false; |
6439 |
return false; |
| 6440 |
|
6440 |
|
| 6441 |
// Check if all users of the add are provably no-wrap. |
6441 |
// Check if all users of the add are provably no-wrap. |
| 6442 |
for (const auto *Result : Results) { |
6442 |
for (const auto *Result : Results) { |
| 6443 |
// If the extractvalue itself is not executed on overflow, the we don't |
6443 |
// If the extractvalue itself is not executed on overflow, the we don't |
| 6444 |
// need to check each use separately, since domination is transitive. |
6444 |
// need to check each use separately, since domination is transitive. |
| 6445 |
if (DT.dominates(NoWrapEdge, Result->getParent())) |
6445 |
if (DT.dominates(NoWrapEdge, Result->getParent())) |
| 6446 |
continue; |
6446 |
continue; |
| 6447 |
|
6447 |
|
| 6448 |
for (const auto &RU : Result->uses()) |
6448 |
for (const auto &RU : Result->uses()) |
| 6449 |
if (!DT.dominates(NoWrapEdge, RU)) |
6449 |
if (!DT.dominates(NoWrapEdge, RU)) |
| 6450 |
return false; |
6450 |
return false; |
| 6451 |
} |
6451 |
} |
| 6452 |
|
6452 |
|
| 6453 |
return true; |
6453 |
return true; |
| 6454 |
}; |
6454 |
}; |
| 6455 |
|
6455 |
|
| 6456 |
return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch); |
6456 |
return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch); |
| 6457 |
} |
6457 |
} |
| 6458 |
|
6458 |
|
| 6459 |
/// Shifts return poison if shiftwidth is larger than the bitwidth. |
6459 |
/// Shifts return poison if shiftwidth is larger than the bitwidth. |
| 6460 |
static bool shiftAmountKnownInRange(const Value *ShiftAmount) { |
6460 |
static bool shiftAmountKnownInRange(const Value *ShiftAmount) { |
| 6461 |
auto *C = dyn_cast(ShiftAmount); |
6461 |
auto *C = dyn_cast(ShiftAmount); |
| 6462 |
if (!C) |
6462 |
if (!C) |
| 6463 |
return false; |
6463 |
return false; |
| 6464 |
|
6464 |
|
| 6465 |
// Shifts return poison if shiftwidth is larger than the bitwidth. |
6465 |
// Shifts return poison if shiftwidth is larger than the bitwidth. |
| 6466 |
SmallVector ShiftAmounts; |
6466 |
SmallVector ShiftAmounts; |
| 6467 |
if (auto *FVTy = dyn_cast(C->getType())) { |
6467 |
if (auto *FVTy = dyn_cast(C->getType())) { |
| 6468 |
unsigned NumElts = FVTy->getNumElements(); |
6468 |
unsigned NumElts = FVTy->getNumElements(); |
| 6469 |
for (unsigned i = 0; i < NumElts; ++i) |
6469 |
for (unsigned i = 0; i < NumElts; ++i) |
| 6470 |
ShiftAmounts.push_back(C->getAggregateElement(i)); |
6470 |
ShiftAmounts.push_back(C->getAggregateElement(i)); |
| 6471 |
} else if (isa(C->getType())) |
6471 |
} else if (isa(C->getType())) |
| 6472 |
return false; // Can't tell, just return false to be safe |
6472 |
return false; // Can't tell, just return false to be safe |
| 6473 |
else |
6473 |
else |
| 6474 |
ShiftAmounts.push_back(C); |
6474 |
ShiftAmounts.push_back(C); |
| 6475 |
|
6475 |
|
| 6476 |
bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) { |
6476 |
bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) { |
| 6477 |
auto *CI = dyn_cast_or_null(C); |
6477 |
auto *CI = dyn_cast_or_null(C); |
| 6478 |
return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth()); |
6478 |
return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth()); |
| 6479 |
}); |
6479 |
}); |
| 6480 |
|
6480 |
|
| 6481 |
return Safe; |
6481 |
return Safe; |
| 6482 |
} |
6482 |
} |
| 6483 |
|
6483 |
|
| 6484 |
static bool canCreateUndefOrPoison(const Operator *Op, bool PoisonOnly, |
6484 |
static bool canCreateUndefOrPoison(const Operator *Op, bool PoisonOnly, |
| 6485 |
bool ConsiderFlagsAndMetadata) { |
6485 |
bool ConsiderFlagsAndMetadata) { |
| 6486 |
|
6486 |
|
| 6487 |
if (ConsiderFlagsAndMetadata && Op->hasPoisonGeneratingFlagsOrMetadata()) |
6487 |
if (ConsiderFlagsAndMetadata && Op->hasPoisonGeneratingFlagsOrMetadata()) |
| 6488 |
return true; |
6488 |
return true; |
| 6489 |
|
6489 |
|
| 6490 |
unsigned Opcode = Op->getOpcode(); |
6490 |
unsigned Opcode = Op->getOpcode(); |
| 6491 |
|
6491 |
|
| 6492 |
// Check whether opcode is a poison/undef-generating operation |
6492 |
// Check whether opcode is a poison/undef-generating operation |
| 6493 |
switch (Opcode) { |
6493 |
switch (Opcode) { |
| 6494 |
case Instruction::Shl: |
6494 |
case Instruction::Shl: |
| 6495 |
case Instruction::AShr: |
6495 |
case Instruction::AShr: |
| 6496 |
case Instruction::LShr: |
6496 |
case Instruction::LShr: |
| 6497 |
return !shiftAmountKnownInRange(Op->getOperand(1)); |
6497 |
return !shiftAmountKnownInRange(Op->getOperand(1)); |
| 6498 |
case Instruction::FPToSI: |
6498 |
case Instruction::FPToSI: |
| 6499 |
case Instruction::FPToUI: |
6499 |
case Instruction::FPToUI: |
| 6500 |
// fptosi/ui yields poison if the resulting value does not fit in the |
6500 |
// fptosi/ui yields poison if the resulting value does not fit in the |
| 6501 |
// destination type. |
6501 |
// destination type. |
| 6502 |
return true; |
6502 |
return true; |
| 6503 |
case Instruction::Call: |
6503 |
case Instruction::Call: |
| 6504 |
if (auto *II = dyn_cast(Op)) { |
6504 |
if (auto *II = dyn_cast(Op)) { |
| 6505 |
switch (II->getIntrinsicID()) { |
6505 |
switch (II->getIntrinsicID()) { |
| 6506 |
// TODO: Add more intrinsics. |
6506 |
// TODO: Add more intrinsics. |
| 6507 |
case Intrinsic::ctlz: |
6507 |
case Intrinsic::ctlz: |
| 6508 |
case Intrinsic::cttz: |
6508 |
case Intrinsic::cttz: |
| 6509 |
case Intrinsic::abs: |
6509 |
case Intrinsic::abs: |
| 6510 |
if (cast(II->getArgOperand(1))->isNullValue()) |
6510 |
if (cast(II->getArgOperand(1))->isNullValue()) |
| 6511 |
return false; |
6511 |
return false; |
| 6512 |
break; |
6512 |
break; |
| 6513 |
case Intrinsic::ctpop: |
6513 |
case Intrinsic::ctpop: |
| 6514 |
case Intrinsic::bswap: |
6514 |
case Intrinsic::bswap: |
| 6515 |
case Intrinsic::bitreverse: |
6515 |
case Intrinsic::bitreverse: |
| 6516 |
case Intrinsic::fshl: |
6516 |
case Intrinsic::fshl: |
| 6517 |
case Intrinsic::fshr: |
6517 |
case Intrinsic::fshr: |
| 6518 |
case Intrinsic::smax: |
6518 |
case Intrinsic::smax: |
| 6519 |
case Intrinsic::smin: |
6519 |
case Intrinsic::smin: |
| 6520 |
case Intrinsic::umax: |
6520 |
case Intrinsic::umax: |
| 6521 |
case Intrinsic::umin: |
6521 |
case Intrinsic::umin: |
| 6522 |
case Intrinsic::ptrmask: |
6522 |
case Intrinsic::ptrmask: |
| 6523 |
case Intrinsic::fptoui_sat: |
6523 |
case Intrinsic::fptoui_sat: |
| 6524 |
case Intrinsic::fptosi_sat: |
6524 |
case Intrinsic::fptosi_sat: |
| 6525 |
case Intrinsic::sadd_with_overflow: |
6525 |
case Intrinsic::sadd_with_overflow: |
| 6526 |
case Intrinsic::ssub_with_overflow: |
6526 |
case Intrinsic::ssub_with_overflow: |
| 6527 |
case Intrinsic::smul_with_overflow: |
6527 |
case Intrinsic::smul_with_overflow: |
| 6528 |
case Intrinsic::uadd_with_overflow: |
6528 |
case Intrinsic::uadd_with_overflow: |
| 6529 |
case Intrinsic::usub_with_overflow: |
6529 |
case Intrinsic::usub_with_overflow: |
| 6530 |
case Intrinsic::umul_with_overflow: |
6530 |
case Intrinsic::umul_with_overflow: |
| 6531 |
case Intrinsic::sadd_sat: |
6531 |
case Intrinsic::sadd_sat: |
| 6532 |
case Intrinsic::uadd_sat: |
6532 |
case Intrinsic::uadd_sat: |
| 6533 |
case Intrinsic::ssub_sat: |
6533 |
case Intrinsic::ssub_sat: |
| 6534 |
case Intrinsic::usub_sat: |
6534 |
case Intrinsic::usub_sat: |
| 6535 |
return false; |
6535 |
return false; |
| 6536 |
case Intrinsic::sshl_sat: |
6536 |
case Intrinsic::sshl_sat: |
| 6537 |
case Intrinsic::ushl_sat: |
6537 |
case Intrinsic::ushl_sat: |
| 6538 |
return !shiftAmountKnownInRange(II->getArgOperand(1)); |
6538 |
return !shiftAmountKnownInRange(II->getArgOperand(1)); |
| 6539 |
case Intrinsic::fma: |
6539 |
case Intrinsic::fma: |
| 6540 |
case Intrinsic::fmuladd: |
6540 |
case Intrinsic::fmuladd: |
| 6541 |
case Intrinsic::sqrt: |
6541 |
case Intrinsic::sqrt: |
| 6542 |
case Intrinsic::powi: |
6542 |
case Intrinsic::powi: |
| 6543 |
case Intrinsic::sin: |
6543 |
case Intrinsic::sin: |
| 6544 |
case Intrinsic::cos: |
6544 |
case Intrinsic::cos: |
| 6545 |
case Intrinsic::pow: |
6545 |
case Intrinsic::pow: |
| 6546 |
case Intrinsic::log: |
6546 |
case Intrinsic::log: |
| 6547 |
case Intrinsic::log10: |
6547 |
case Intrinsic::log10: |
| 6548 |
case Intrinsic::log2: |
6548 |
case Intrinsic::log2: |
| 6549 |
case Intrinsic::exp: |
6549 |
case Intrinsic::exp: |
| 6550 |
case Intrinsic::exp2: |
6550 |
case Intrinsic::exp2: |
| 6551 |
case Intrinsic::fabs: |
6551 |
case Intrinsic::fabs: |
| 6552 |
case Intrinsic::copysign: |
6552 |
case Intrinsic::copysign: |
| 6553 |
case Intrinsic::floor: |
6553 |
case Intrinsic::floor: |
| 6554 |
case Intrinsic::ceil: |
6554 |
case Intrinsic::ceil: |
| 6555 |
case Intrinsic::trunc: |
6555 |
case Intrinsic::trunc: |
| 6556 |
case Intrinsic::rint: |
6556 |
case Intrinsic::rint: |
| 6557 |
case Intrinsic::nearbyint: |
6557 |
case Intrinsic::nearbyint: |
| 6558 |
case Intrinsic::round: |
6558 |
case Intrinsic::round: |
| 6559 |
case Intrinsic::roundeven: |
6559 |
case Intrinsic::roundeven: |
| 6560 |
case Intrinsic::fptrunc_round: |
6560 |
case Intrinsic::fptrunc_round: |
| 6561 |
case Intrinsic::canonicalize: |
6561 |
case Intrinsic::canonicalize: |
| 6562 |
case Intrinsic::arithmetic_fence: |
6562 |
case Intrinsic::arithmetic_fence: |
| 6563 |
case Intrinsic::minnum: |
6563 |
case Intrinsic::minnum: |
| 6564 |
case Intrinsic::maxnum: |
6564 |
case Intrinsic::maxnum: |
| 6565 |
case Intrinsic::minimum: |
6565 |
case Intrinsic::minimum: |
| 6566 |
case Intrinsic::maximum: |
6566 |
case Intrinsic::maximum: |
| 6567 |
case Intrinsic::is_fpclass: |
6567 |
case Intrinsic::is_fpclass: |
| 6568 |
return false; |
6568 |
return false; |
| 6569 |
case Intrinsic::lround: |
6569 |
case Intrinsic::lround: |
| 6570 |
case Intrinsic::llround: |
6570 |
case Intrinsic::llround: |
| 6571 |
case Intrinsic::lrint: |
6571 |
case Intrinsic::lrint: |
| 6572 |
case Intrinsic::llrint: |
6572 |
case Intrinsic::llrint: |
| 6573 |
// If the value doesn't fit an unspecified value is returned (but this |
6573 |
// If the value doesn't fit an unspecified value is returned (but this |
| 6574 |
// is not poison). |
6574 |
// is not poison). |
| 6575 |
return false; |
6575 |
return false; |
| 6576 |
} |
6576 |
} |
| 6577 |
} |
6577 |
} |
| 6578 |
[[fallthrough]]; |
6578 |
[[fallthrough]]; |
| 6579 |
case Instruction::CallBr: |
6579 |
case Instruction::CallBr: |
| 6580 |
case Instruction::Invoke: { |
6580 |
case Instruction::Invoke: { |
| 6581 |
const auto *CB = cast(Op); |
6581 |
const auto *CB = cast(Op); |
| 6582 |
return !CB->hasRetAttr(Attribute::NoUndef); |
6582 |
return !CB->hasRetAttr(Attribute::NoUndef); |
| 6583 |
} |
6583 |
} |
| 6584 |
case Instruction::InsertElement: |
6584 |
case Instruction::InsertElement: |
| 6585 |
case Instruction::ExtractElement: { |
6585 |
case Instruction::ExtractElement: { |
| 6586 |
// If index exceeds the length of the vector, it returns poison |
6586 |
// If index exceeds the length of the vector, it returns poison |
| 6587 |
auto *VTy = cast(Op->getOperand(0)->getType()); |
6587 |
auto *VTy = cast(Op->getOperand(0)->getType()); |
| 6588 |
unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1; |
6588 |
unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1; |
| 6589 |
auto *Idx = dyn_cast(Op->getOperand(IdxOp)); |
6589 |
auto *Idx = dyn_cast(Op->getOperand(IdxOp)); |
| 6590 |
if (!Idx || Idx->getValue().uge(VTy->getElementCount().getKnownMinValue())) |
6590 |
if (!Idx || Idx->getValue().uge(VTy->getElementCount().getKnownMinValue())) |
| 6591 |
return true; |
6591 |
return true; |
| 6592 |
return false; |
6592 |
return false; |
| 6593 |
} |
6593 |
} |
| 6594 |
case Instruction::ShuffleVector: { |
6594 |
case Instruction::ShuffleVector: { |
| 6595 |
// shufflevector may return undef. |
6595 |
// shufflevector may return undef. |
| 6596 |
if (PoisonOnly) |
6596 |
if (PoisonOnly) |
| 6597 |
return false; |
6597 |
return false; |
| 6598 |
ArrayRef Mask = isa(Op) |
6598 |
ArrayRef Mask = isa(Op) |
| 6599 |
? cast(Op)->getShuffleMask() |
6599 |
? cast(Op)->getShuffleMask() |
| 6600 |
: cast(Op)->getShuffleMask(); |
6600 |
: cast(Op)->getShuffleMask(); |
| 6601 |
return is_contained(Mask, PoisonMaskElem); |
6601 |
return is_contained(Mask, PoisonMaskElem); |
| 6602 |
} |
6602 |
} |
| 6603 |
case Instruction::FNeg: |
6603 |
case Instruction::FNeg: |
| 6604 |
case Instruction::PHI: |
6604 |
case Instruction::PHI: |
| 6605 |
case Instruction::Select: |
6605 |
case Instruction::Select: |
| 6606 |
case Instruction::URem: |
6606 |
case Instruction::URem: |
| 6607 |
case Instruction::SRem: |
6607 |
case Instruction::SRem: |
| 6608 |
case Instruction::ExtractValue: |
6608 |
case Instruction::ExtractValue: |
| 6609 |
case Instruction::InsertValue: |
6609 |
case Instruction::InsertValue: |
| 6610 |
case Instruction::Freeze: |
6610 |
case Instruction::Freeze: |
| 6611 |
case Instruction::ICmp: |
6611 |
case Instruction::ICmp: |
| 6612 |
case Instruction::FCmp: |
6612 |
case Instruction::FCmp: |
| 6613 |
case Instruction::FAdd: |
6613 |
case Instruction::FAdd: |
| 6614 |
case Instruction::FSub: |
6614 |
case Instruction::FSub: |
| 6615 |
case Instruction::FMul: |
6615 |
case Instruction::FMul: |
| 6616 |
case Instruction::FDiv: |
6616 |
case Instruction::FDiv: |
| 6617 |
case Instruction::FRem: |
6617 |
case Instruction::FRem: |
| 6618 |
return false; |
6618 |
return false; |
| 6619 |
case Instruction::GetElementPtr: |
6619 |
case Instruction::GetElementPtr: |
| 6620 |
// inbounds is handled above |
6620 |
// inbounds is handled above |
| 6621 |
// TODO: what about inrange on constexpr? |
6621 |
// TODO: what about inrange on constexpr? |
| 6622 |
return false; |
6622 |
return false; |
| 6623 |
default: { |
6623 |
default: { |
| 6624 |
const auto *CE = dyn_cast(Op); |
6624 |
const auto *CE = dyn_cast(Op); |
| 6625 |
if (isa(Op) || (CE && CE->isCast())) |
6625 |
if (isa(Op) || (CE && CE->isCast())) |
| 6626 |
return false; |
6626 |
return false; |
| 6627 |
else if (Instruction::isBinaryOp(Opcode)) |
6627 |
else if (Instruction::isBinaryOp(Opcode)) |
| 6628 |
return false; |
6628 |
return false; |
| 6629 |
// Be conservative and return true. |
6629 |
// Be conservative and return true. |
| 6630 |
return true; |
6630 |
return true; |
| 6631 |
} |
6631 |
} |
| 6632 |
} |
6632 |
} |
| 6633 |
} |
6633 |
} |
| 6634 |
|
6634 |
|
| 6635 |
bool llvm::canCreateUndefOrPoison(const Operator *Op, |
6635 |
bool llvm::canCreateUndefOrPoison(const Operator *Op, |
| 6636 |
bool ConsiderFlagsAndMetadata) { |
6636 |
bool ConsiderFlagsAndMetadata) { |
| 6637 |
return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/false, |
6637 |
return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/false, |
| 6638 |
ConsiderFlagsAndMetadata); |
6638 |
ConsiderFlagsAndMetadata); |
| 6639 |
} |
6639 |
} |
| 6640 |
|
6640 |
|
| 6641 |
bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) { |
6641 |
bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) { |
| 6642 |
return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/true, |
6642 |
return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/true, |
| 6643 |
ConsiderFlagsAndMetadata); |
6643 |
ConsiderFlagsAndMetadata); |
| 6644 |
} |
6644 |
} |
| 6645 |
|
6645 |
|
| 6646 |
static bool directlyImpliesPoison(const Value *ValAssumedPoison, |
6646 |
static bool directlyImpliesPoison(const Value *ValAssumedPoison, |
| 6647 |
const Value *V, unsigned Depth) { |
6647 |
const Value *V, unsigned Depth) { |
| 6648 |
if (ValAssumedPoison == V) |
6648 |
if (ValAssumedPoison == V) |
| 6649 |
return true; |
6649 |
return true; |
| 6650 |
|
6650 |
|
| 6651 |
const unsigned MaxDepth = 2; |
6651 |
const unsigned MaxDepth = 2; |
| 6652 |
if (Depth >= MaxDepth) |
6652 |
if (Depth >= MaxDepth) |
| 6653 |
return false; |
6653 |
return false; |
| 6654 |
|
6654 |
|
| 6655 |
if (const auto *I = dyn_cast(V)) { |
6655 |
if (const auto *I = dyn_cast(V)) { |
| 6656 |
if (any_of(I->operands(), [=](const Use &Op) { |
6656 |
if (any_of(I->operands(), [=](const Use &Op) { |
| 6657 |
return propagatesPoison(Op) && |
6657 |
return propagatesPoison(Op) && |
| 6658 |
directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1); |
6658 |
directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1); |
| 6659 |
})) |
6659 |
})) |
| 6660 |
return true; |
6660 |
return true; |
| 6661 |
|
6661 |
|
| 6662 |
// V = extractvalue V0, idx |
6662 |
// V = extractvalue V0, idx |
| 6663 |
// V2 = extractvalue V0, idx2 |
6663 |
// V2 = extractvalue V0, idx2 |
| 6664 |
// V0's elements are all poison or not. (e.g., add_with_overflow) |
6664 |
// V0's elements are all poison or not. (e.g., add_with_overflow) |
| 6665 |
const WithOverflowInst *II; |
6665 |
const WithOverflowInst *II; |
| 6666 |
if (match(I, m_ExtractValue(m_WithOverflowInst(II))) && |
6666 |
if (match(I, m_ExtractValue(m_WithOverflowInst(II))) && |
| 6667 |
(match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) || |
6667 |
(match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) || |
| 6668 |
llvm::is_contained(II->args(), ValAssumedPoison))) |
6668 |
llvm::is_contained(II->args(), ValAssumedPoison))) |
| 6669 |
return true; |
6669 |
return true; |
| 6670 |
} |
6670 |
} |
| 6671 |
return false; |
6671 |
return false; |
| 6672 |
} |
6672 |
} |
| 6673 |
|
6673 |
|
| 6674 |
static bool impliesPoison(const Value *ValAssumedPoison, const Value *V, |
6674 |
static bool impliesPoison(const Value *ValAssumedPoison, const Value *V, |
| 6675 |
unsigned Depth) { |
6675 |
unsigned Depth) { |
| 6676 |
if (isGuaranteedNotToBePoison(ValAssumedPoison)) |
6676 |
if (isGuaranteedNotToBePoison(ValAssumedPoison)) |
| 6677 |
return true; |
6677 |
return true; |
| 6678 |
|
6678 |
|
| 6679 |
if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0)) |
6679 |
if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0)) |
| 6680 |
return true; |
6680 |
return true; |
| 6681 |
|
6681 |
|
| 6682 |
const unsigned MaxDepth = 2; |
6682 |
const unsigned MaxDepth = 2; |
| 6683 |
if (Depth >= MaxDepth) |
6683 |
if (Depth >= MaxDepth) |
| 6684 |
return false; |
6684 |
return false; |
| 6685 |
|
6685 |
|
| 6686 |
const auto *I = dyn_cast(ValAssumedPoison); |
6686 |
const auto *I = dyn_cast(ValAssumedPoison); |
| 6687 |
if (I && !canCreatePoison(cast(I))) { |
6687 |
if (I && !canCreatePoison(cast(I))) { |
| 6688 |
return all_of(I->operands(), [=](const Value *Op) { |
6688 |
return all_of(I->operands(), [=](const Value *Op) { |
| 6689 |
return impliesPoison(Op, V, Depth + 1); |
6689 |
return impliesPoison(Op, V, Depth + 1); |
| 6690 |
}); |
6690 |
}); |
| 6691 |
} |
6691 |
} |
| 6692 |
return false; |
6692 |
return false; |
| 6693 |
} |
6693 |
} |
| 6694 |
|
6694 |
|
| 6695 |
bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) { |
6695 |
bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) { |
| 6696 |
return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0); |
6696 |
return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0); |
| 6697 |
} |
6697 |
} |
| 6698 |
|
6698 |
|
| 6699 |
static bool programUndefinedIfUndefOrPoison(const Value *V, |
6699 |
static bool programUndefinedIfUndefOrPoison(const Value *V, |
| 6700 |
bool PoisonOnly); |
6700 |
bool PoisonOnly); |
| 6701 |
|
6701 |
|
| 6702 |
static bool isGuaranteedNotToBeUndefOrPoison(const Value *V, |
6702 |
static bool isGuaranteedNotToBeUndefOrPoison(const Value *V, |
| 6703 |
AssumptionCache *AC, |
6703 |
AssumptionCache *AC, |
| 6704 |
const Instruction *CtxI, |
6704 |
const Instruction *CtxI, |
| 6705 |
const DominatorTree *DT, |
6705 |
const DominatorTree *DT, |
| 6706 |
unsigned Depth, bool PoisonOnly) { |
6706 |
unsigned Depth, bool PoisonOnly) { |
| 6707 |
if (Depth >= MaxAnalysisRecursionDepth) |
6707 |
if (Depth >= MaxAnalysisRecursionDepth) |
| 6708 |
return false; |
6708 |
return false; |
| 6709 |
|
6709 |
|
| 6710 |
if (isa(V)) |
6710 |
if (isa(V)) |
| 6711 |
return false; |
6711 |
return false; |
| 6712 |
|
6712 |
|
| 6713 |
if (const auto *A = dyn_cast(V)) { |
6713 |
if (const auto *A = dyn_cast(V)) { |
| 6714 |
if (A->hasAttribute(Attribute::NoUndef) || |
6714 |
if (A->hasAttribute(Attribute::NoUndef) || |
| 6715 |
A->hasAttribute(Attribute::Dereferenceable) || |
6715 |
A->hasAttribute(Attribute::Dereferenceable) || |
| 6716 |
A->hasAttribute(Attribute::DereferenceableOrNull)) |
6716 |
A->hasAttribute(Attribute::DereferenceableOrNull)) |
| 6717 |
return true; |
6717 |
return true; |
| 6718 |
} |
6718 |
} |
| 6719 |
|
6719 |
|
| 6720 |
if (auto *C = dyn_cast(V)) { |
6720 |
if (auto *C = dyn_cast(V)) { |
| 6721 |
if (isa(C)) |
6721 |
if (isa(C)) |
| 6722 |
return PoisonOnly && !isa(C); |
6722 |
return PoisonOnly && !isa(C); |
| 6723 |
|
6723 |
|
| 6724 |
if (isa(C) || isa(C) || isa(V) || |
6724 |
if (isa(C) || isa(C) || isa(V) || |
| 6725 |
isa(C) || isa(C)) |
6725 |
isa(C) || isa(C)) |
| 6726 |
return true; |
6726 |
return true; |
| 6727 |
|
6727 |
|
| 6728 |
if (C->getType()->isVectorTy() && !isa(C)) |
6728 |
if (C->getType()->isVectorTy() && !isa(C)) |
| 6729 |
return (PoisonOnly ? !C->containsPoisonElement() |
6729 |
return (PoisonOnly ? !C->containsPoisonElement() |
| 6730 |
: !C->containsUndefOrPoisonElement()) && |
6730 |
: !C->containsUndefOrPoisonElement()) && |
| 6731 |
!C->containsConstantExpression(); |
6731 |
!C->containsConstantExpression(); |
| 6732 |
} |
6732 |
} |
| 6733 |
|
6733 |
|
| 6734 |
// Strip cast operations from a pointer value. |
6734 |
// Strip cast operations from a pointer value. |
| 6735 |
// Note that stripPointerCastsSameRepresentation can strip off getelementptr |
6735 |
// Note that stripPointerCastsSameRepresentation can strip off getelementptr |
| 6736 |
// inbounds with zero offset. To guarantee that the result isn't poison, the |
6736 |
// inbounds with zero offset. To guarantee that the result isn't poison, the |
| 6737 |
// stripped pointer is checked as it has to be pointing into an allocated |
6737 |
// stripped pointer is checked as it has to be pointing into an allocated |
| 6738 |
// object or be null `null` to ensure `inbounds` getelement pointers with a |
6738 |
// object or be null `null` to ensure `inbounds` getelement pointers with a |
| 6739 |
// zero offset could not produce poison. |
6739 |
// zero offset could not produce poison. |
| 6740 |
// It can strip off addrspacecast that do not change bit representation as |
6740 |
// It can strip off addrspacecast that do not change bit representation as |
| 6741 |
// well. We believe that such addrspacecast is equivalent to no-op. |
6741 |
// well. We believe that such addrspacecast is equivalent to no-op. |
| 6742 |
auto *StrippedV = V->stripPointerCastsSameRepresentation(); |
6742 |
auto *StrippedV = V->stripPointerCastsSameRepresentation(); |
| 6743 |
if (isa(StrippedV) || isa(StrippedV) || |
6743 |
if (isa(StrippedV) || isa(StrippedV) || |
| 6744 |
isa(StrippedV) || isa(StrippedV)) |
6744 |
isa(StrippedV) || isa(StrippedV)) |
| 6745 |
return true; |
6745 |
return true; |
| 6746 |
|
6746 |
|
| 6747 |
auto OpCheck = [&](const Value *V) { |
6747 |
auto OpCheck = [&](const Value *V) { |
| 6748 |
return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, |
6748 |
return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, |
| 6749 |
PoisonOnly); |
6749 |
PoisonOnly); |
| 6750 |
}; |
6750 |
}; |
| 6751 |
|
6751 |
|
| 6752 |
if (auto *Opr = dyn_cast(V)) { |
6752 |
if (auto *Opr = dyn_cast(V)) { |
| 6753 |
// If the value is a freeze instruction, then it can never |
6753 |
// If the value is a freeze instruction, then it can never |
| 6754 |
// be undef or poison. |
6754 |
// be undef or poison. |
| 6755 |
if (isa(V)) |
6755 |
if (isa(V)) |
| 6756 |
return true; |
6756 |
return true; |
| 6757 |
|
6757 |
|
| 6758 |
if (const auto *CB = dyn_cast(V)) { |
6758 |
if (const auto *CB = dyn_cast(V)) { |
| 6759 |
if (CB->hasRetAttr(Attribute::NoUndef)) |
6759 |
if (CB->hasRetAttr(Attribute::NoUndef)) |
| 6760 |
return true; |
6760 |
return true; |
| 6761 |
} |
6761 |
} |
| 6762 |
|
6762 |
|
| 6763 |
if (const auto *PN = dyn_cast(V)) { |
6763 |
if (const auto *PN = dyn_cast(V)) { |
| 6764 |
unsigned Num = PN->getNumIncomingValues(); |
6764 |
unsigned Num = PN->getNumIncomingValues(); |
| 6765 |
bool IsWellDefined = true; |
6765 |
bool IsWellDefined = true; |
| 6766 |
for (unsigned i = 0; i < Num; ++i) { |
6766 |
for (unsigned i = 0; i < Num; ++i) { |
| 6767 |
auto *TI = PN->getIncomingBlock(i)->getTerminator(); |
6767 |
auto *TI = PN->getIncomingBlock(i)->getTerminator(); |
| 6768 |
if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI, |
6768 |
if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI, |
| 6769 |
DT, Depth + 1, PoisonOnly)) { |
6769 |
DT, Depth + 1, PoisonOnly)) { |
| 6770 |
IsWellDefined = false; |
6770 |
IsWellDefined = false; |
| 6771 |
break; |
6771 |
break; |
| 6772 |
} |
6772 |
} |
| 6773 |
} |
6773 |
} |
| 6774 |
if (IsWellDefined) |
6774 |
if (IsWellDefined) |
| 6775 |
return true; |
6775 |
return true; |
| 6776 |
} else if (!canCreateUndefOrPoison(Opr) && all_of(Opr->operands(), OpCheck)) |
6776 |
} else if (!canCreateUndefOrPoison(Opr) && all_of(Opr->operands(), OpCheck)) |
| 6777 |
return true; |
6777 |
return true; |
| 6778 |
} |
6778 |
} |
| 6779 |
|
6779 |
|
| 6780 |
if (auto *I = dyn_cast(V)) |
6780 |
if (auto *I = dyn_cast(V)) |
| 6781 |
if (I->hasMetadata(LLVMContext::MD_noundef) || |
6781 |
if (I->hasMetadata(LLVMContext::MD_noundef) || |
| 6782 |
I->hasMetadata(LLVMContext::MD_dereferenceable) || |
6782 |
I->hasMetadata(LLVMContext::MD_dereferenceable) || |
| 6783 |
I->hasMetadata(LLVMContext::MD_dereferenceable_or_null)) |
6783 |
I->hasMetadata(LLVMContext::MD_dereferenceable_or_null)) |
| 6784 |
return true; |
6784 |
return true; |
| 6785 |
|
6785 |
|
| 6786 |
if (programUndefinedIfUndefOrPoison(V, PoisonOnly)) |
6786 |
if (programUndefinedIfUndefOrPoison(V, PoisonOnly)) |
| 6787 |
return true; |
6787 |
return true; |
| 6788 |
|
6788 |
|
| 6789 |
// CxtI may be null or a cloned instruction. |
6789 |
// CxtI may be null or a cloned instruction. |
| 6790 |
if (!CtxI || !CtxI->getParent() || !DT) |
6790 |
if (!CtxI || !CtxI->getParent() || !DT) |
| 6791 |
return false; |
6791 |
return false; |
| 6792 |
|
6792 |
|
| 6793 |
auto *DNode = DT->getNode(CtxI->getParent()); |
6793 |
auto *DNode = DT->getNode(CtxI->getParent()); |
| 6794 |
if (!DNode) |
6794 |
if (!DNode) |
| 6795 |
// Unreachable block |
6795 |
// Unreachable block |
| 6796 |
return false; |
6796 |
return false; |
| 6797 |
|
6797 |
|
| 6798 |
// If V is used as a branch condition before reaching CtxI, V cannot be |
6798 |
// If V is used as a branch condition before reaching CtxI, V cannot be |
| 6799 |
// undef or poison. |
6799 |
// undef or poison. |
| 6800 |
// br V, BB1, BB2 |
6800 |
// br V, BB1, BB2 |
| 6801 |
// BB1: |
6801 |
// BB1: |
| 6802 |
// CtxI ; V cannot be undef or poison here |
6802 |
// CtxI ; V cannot be undef or poison here |
| 6803 |
auto *Dominator = DNode->getIDom(); |
6803 |
auto *Dominator = DNode->getIDom(); |
| 6804 |
while (Dominator) { |
6804 |
while (Dominator) { |
| 6805 |
auto *TI = Dominator->getBlock()->getTerminator(); |
6805 |
auto *TI = Dominator->getBlock()->getTerminator(); |
| 6806 |
|
6806 |
|
| 6807 |
Value *Cond = nullptr; |
6807 |
Value *Cond = nullptr; |
| 6808 |
if (auto BI = dyn_cast_or_null(TI)) { |
6808 |
if (auto BI = dyn_cast_or_null(TI)) { |
| 6809 |
if (BI->isConditional()) |
6809 |
if (BI->isConditional()) |
| 6810 |
Cond = BI->getCondition(); |
6810 |
Cond = BI->getCondition(); |
| 6811 |
} else if (auto SI = dyn_cast_or_null(TI)) { |
6811 |
} else if (auto SI = dyn_cast_or_null(TI)) { |
| 6812 |
Cond = SI->getCondition(); |
6812 |
Cond = SI->getCondition(); |
| 6813 |
} |
6813 |
} |
| 6814 |
|
6814 |
|
| 6815 |
if (Cond) { |
6815 |
if (Cond) { |
| 6816 |
if (Cond == V) |
6816 |
if (Cond == V) |
| 6817 |
return true; |
6817 |
return true; |
| 6818 |
else if (PoisonOnly && isa(Cond)) { |
6818 |
else if (PoisonOnly && isa(Cond)) { |
| 6819 |
// For poison, we can analyze further |
6819 |
// For poison, we can analyze further |
| 6820 |
auto *Opr = cast(Cond); |
6820 |
auto *Opr = cast(Cond); |
| 6821 |
if (any_of(Opr->operands(), |
6821 |
if (any_of(Opr->operands(), |
| 6822 |
[V](const Use &U) { return V == U && propagatesPoison(U); })) |
6822 |
[V](const Use &U) { return V == U && propagatesPoison(U); })) |
| 6823 |
return true; |
6823 |
return true; |
| 6824 |
} |
6824 |
} |
| 6825 |
} |
6825 |
} |
| 6826 |
|
6826 |
|
| 6827 |
Dominator = Dominator->getIDom(); |
6827 |
Dominator = Dominator->getIDom(); |
| 6828 |
} |
6828 |
} |
| 6829 |
|
6829 |
|
| 6830 |
if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC)) |
6830 |
if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC)) |
| 6831 |
return true; |
6831 |
return true; |
| 6832 |
|
6832 |
|
| 6833 |
return false; |
6833 |
return false; |
| 6834 |
} |
6834 |
} |
| 6835 |
|
6835 |
|
| 6836 |
bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC, |
6836 |
bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC, |
| 6837 |
const Instruction *CtxI, |
6837 |
const Instruction *CtxI, |
| 6838 |
const DominatorTree *DT, |
6838 |
const DominatorTree *DT, |
| 6839 |
unsigned Depth) { |
6839 |
unsigned Depth) { |
| 6840 |
return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, false); |
6840 |
return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, false); |
| 6841 |
} |
6841 |
} |
| 6842 |
|
6842 |
|
| 6843 |
bool llvm::isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC, |
6843 |
bool llvm::isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC, |
| 6844 |
const Instruction *CtxI, |
6844 |
const Instruction *CtxI, |
| 6845 |
const DominatorTree *DT, unsigned Depth) { |
6845 |
const DominatorTree *DT, unsigned Depth) { |
| 6846 |
return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, true); |
6846 |
return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, true); |
| 6847 |
} |
6847 |
} |
| 6848 |
|
6848 |
|
| 6849 |
/// Return true if undefined behavior would provably be executed on the path to |
6849 |
/// Return true if undefined behavior would provably be executed on the path to |
| 6850 |
/// OnPathTo if Root produced a posion result. Note that this doesn't say |
6850 |
/// OnPathTo if Root produced a posion result. Note that this doesn't say |
| 6851 |
/// anything about whether OnPathTo is actually executed or whether Root is |
6851 |
/// anything about whether OnPathTo is actually executed or whether Root is |
| 6852 |
/// actually poison. This can be used to assess whether a new use of Root can |
6852 |
/// actually poison. This can be used to assess whether a new use of Root can |
| 6853 |
/// be added at a location which is control equivalent with OnPathTo (such as |
6853 |
/// be added at a location which is control equivalent with OnPathTo (such as |
| 6854 |
/// immediately before it) without introducing UB which didn't previously |
6854 |
/// immediately before it) without introducing UB which didn't previously |
| 6855 |
/// exist. Note that a false result conveys no information. |
6855 |
/// exist. Note that a false result conveys no information. |
| 6856 |
bool llvm::mustExecuteUBIfPoisonOnPathTo(Instruction *Root, |
6856 |
bool llvm::mustExecuteUBIfPoisonOnPathTo(Instruction *Root, |
| 6857 |
Instruction *OnPathTo, |
6857 |
Instruction *OnPathTo, |
| 6858 |
DominatorTree *DT) { |
6858 |
DominatorTree *DT) { |
| 6859 |
// Basic approach is to assume Root is poison, propagate poison forward |
6859 |
// Basic approach is to assume Root is poison, propagate poison forward |
| 6860 |
// through all users we can easily track, and then check whether any of those |
6860 |
// through all users we can easily track, and then check whether any of those |
| 6861 |
// users are provable UB and must execute before out exiting block might |
6861 |
// users are provable UB and must execute before out exiting block might |
| 6862 |
// exit. |
6862 |
// exit. |
| 6863 |
|
6863 |
|
| 6864 |
// The set of all recursive users we've visited (which are assumed to all be |
6864 |
// The set of all recursive users we've visited (which are assumed to all be |
| 6865 |
// poison because of said visit) |
6865 |
// poison because of said visit) |
| 6866 |
SmallSet KnownPoison; |
6866 |
SmallSet KnownPoison; |
| 6867 |
SmallVector Worklist; |
6867 |
SmallVector Worklist; |
| 6868 |
Worklist.push_back(Root); |
6868 |
Worklist.push_back(Root); |
| 6869 |
while (!Worklist.empty()) { |
6869 |
while (!Worklist.empty()) { |
| 6870 |
const Instruction *I = Worklist.pop_back_val(); |
6870 |
const Instruction *I = Worklist.pop_back_val(); |
| 6871 |
|
6871 |
|
| 6872 |
// If we know this must trigger UB on a path leading our target. |
6872 |
// If we know this must trigger UB on a path leading our target. |
| 6873 |
if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo)) |
6873 |
if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo)) |
| 6874 |
return true; |
6874 |
return true; |
| 6875 |
|
6875 |
|
| 6876 |
// If we can't analyze propagation through this instruction, just skip it |
6876 |
// If we can't analyze propagation through this instruction, just skip it |
| 6877 |
// and transitive users. Safe as false is a conservative result. |
6877 |
// and transitive users. Safe as false is a conservative result. |
| 6878 |
if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) { |
6878 |
if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) { |
| 6879 |
return KnownPoison.contains(U) && propagatesPoison(U); |
6879 |
return KnownPoison.contains(U) && propagatesPoison(U); |
| 6880 |
})) |
6880 |
})) |
| 6881 |
continue; |
6881 |
continue; |
| 6882 |
|
6882 |
|
| 6883 |
if (KnownPoison.insert(I).second) |
6883 |
if (KnownPoison.insert(I).second) |
| 6884 |
for (const User *User : I->users()) |
6884 |
for (const User *User : I->users()) |
| 6885 |
Worklist.push_back(cast(User)); |
6885 |
Worklist.push_back(cast(User)); |
| 6886 |
} |
6886 |
} |
| 6887 |
|
6887 |
|
| 6888 |
// Might be non-UB, or might have a path we couldn't prove must execute on |
6888 |
// Might be non-UB, or might have a path we couldn't prove must execute on |
| 6889 |
// way to exiting bb. |
6889 |
// way to exiting bb. |
| 6890 |
return false; |
6890 |
return false; |
| 6891 |
} |
6891 |
} |
| 6892 |
|
6892 |
|
| 6893 |
OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add, |
6893 |
OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add, |
| 6894 |
const DataLayout &DL, |
6894 |
const DataLayout &DL, |
| 6895 |
AssumptionCache *AC, |
6895 |
AssumptionCache *AC, |
| 6896 |
const Instruction *CxtI, |
6896 |
const Instruction *CxtI, |
| 6897 |
const DominatorTree *DT) { |
6897 |
const DominatorTree *DT) { |
| 6898 |
return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1), |
6898 |
return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1), |
| 6899 |
Add, DL, AC, CxtI, DT); |
6899 |
Add, DL, AC, CxtI, DT); |
| 6900 |
} |
6900 |
} |
| 6901 |
|
6901 |
|
| 6902 |
OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS, |
6902 |
OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS, |
| 6903 |
const Value *RHS, |
6903 |
const Value *RHS, |
| 6904 |
const DataLayout &DL, |
6904 |
const DataLayout &DL, |
| 6905 |
AssumptionCache *AC, |
6905 |
AssumptionCache *AC, |
| 6906 |
const Instruction *CxtI, |
6906 |
const Instruction *CxtI, |
| 6907 |
const DominatorTree *DT) { |
6907 |
const DominatorTree *DT) { |
| 6908 |
return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT); |
6908 |
return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT); |
| 6909 |
} |
6909 |
} |
| 6910 |
|
6910 |
|
| 6911 |
bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) { |
6911 |
bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) { |
| 6912 |
// Note: An atomic operation isn't guaranteed to return in a reasonable amount |
6912 |
// Note: An atomic operation isn't guaranteed to return in a reasonable amount |
| 6913 |
// of time because it's possible for another thread to interfere with it for an |
6913 |
// of time because it's possible for another thread to interfere with it for an |
| 6914 |
// arbitrary length of time, but programs aren't allowed to rely on that. |
6914 |
// arbitrary length of time, but programs aren't allowed to rely on that. |
| 6915 |
|
6915 |
|
| 6916 |
// If there is no successor, then execution can't transfer to it. |
6916 |
// If there is no successor, then execution can't transfer to it. |
| 6917 |
if (isa(I)) |
6917 |
if (isa(I)) |
| 6918 |
return false; |
6918 |
return false; |
| 6919 |
if (isa(I)) |
6919 |
if (isa(I)) |
| 6920 |
return false; |
6920 |
return false; |
| 6921 |
|
6921 |
|
| 6922 |
// Note: Do not add new checks here; instead, change Instruction::mayThrow or |
6922 |
// Note: Do not add new checks here; instead, change Instruction::mayThrow or |
| 6923 |
// Instruction::willReturn. |
6923 |
// Instruction::willReturn. |
| 6924 |
// |
6924 |
// |
| 6925 |
// FIXME: Move this check into Instruction::willReturn. |
6925 |
// FIXME: Move this check into Instruction::willReturn. |
| 6926 |
if (isa(I)) { |
6926 |
if (isa(I)) { |
| 6927 |
switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) { |
6927 |
switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) { |
| 6928 |
default: |
6928 |
default: |
| 6929 |
// A catchpad may invoke exception object constructors and such, which |
6929 |
// A catchpad may invoke exception object constructors and such, which |
| 6930 |
// in some languages can be arbitrary code, so be conservative by default. |
6930 |
// in some languages can be arbitrary code, so be conservative by default. |
| 6931 |
return false; |
6931 |
return false; |
| 6932 |
case EHPersonality::CoreCLR: |
6932 |
case EHPersonality::CoreCLR: |
| 6933 |
// For CoreCLR, it just involves a type test. |
6933 |
// For CoreCLR, it just involves a type test. |
| 6934 |
return true; |
6934 |
return true; |
| 6935 |
} |
6935 |
} |
| 6936 |
} |
6936 |
} |
| 6937 |
|
6937 |
|
| 6938 |
// An instruction that returns without throwing must transfer control flow |
6938 |
// An instruction that returns without throwing must transfer control flow |
| 6939 |
// to a successor. |
6939 |
// to a successor. |
| 6940 |
return !I->mayThrow() && I->willReturn(); |
6940 |
return !I->mayThrow() && I->willReturn(); |
| 6941 |
} |
6941 |
} |
| 6942 |
|
6942 |
|
| 6943 |
bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) { |
6943 |
bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) { |
| 6944 |
// TODO: This is slightly conservative for invoke instruction since exiting |
6944 |
// TODO: This is slightly conservative for invoke instruction since exiting |
| 6945 |
// via an exception *is* normal control for them. |
6945 |
// via an exception *is* normal control for them. |
| 6946 |
for (const Instruction &I : *BB) |
6946 |
for (const Instruction &I : *BB) |
| 6947 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
6947 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
| 6948 |
return false; |
6948 |
return false; |
| 6949 |
return true; |
6949 |
return true; |
| 6950 |
} |
6950 |
} |
| 6951 |
|
6951 |
|
| 6952 |
bool llvm::isGuaranteedToTransferExecutionToSuccessor( |
6952 |
bool llvm::isGuaranteedToTransferExecutionToSuccessor( |
| 6953 |
BasicBlock::const_iterator Begin, BasicBlock::const_iterator End, |
6953 |
BasicBlock::const_iterator Begin, BasicBlock::const_iterator End, |
| 6954 |
unsigned ScanLimit) { |
6954 |
unsigned ScanLimit) { |
| 6955 |
return isGuaranteedToTransferExecutionToSuccessor(make_range(Begin, End), |
6955 |
return isGuaranteedToTransferExecutionToSuccessor(make_range(Begin, End), |
| 6956 |
ScanLimit); |
6956 |
ScanLimit); |
| 6957 |
} |
6957 |
} |
| 6958 |
|
6958 |
|
| 6959 |
bool llvm::isGuaranteedToTransferExecutionToSuccessor( |
6959 |
bool llvm::isGuaranteedToTransferExecutionToSuccessor( |
| 6960 |
iterator_range Range, unsigned ScanLimit) { |
6960 |
iterator_range Range, unsigned ScanLimit) { |
| 6961 |
assert(ScanLimit && "scan limit must be non-zero"); |
6961 |
assert(ScanLimit && "scan limit must be non-zero"); |
| 6962 |
for (const Instruction &I : Range) { |
6962 |
for (const Instruction &I : Range) { |
| 6963 |
if (isa(I)) |
6963 |
if (isa(I)) |
| 6964 |
continue; |
6964 |
continue; |
| 6965 |
if (--ScanLimit == 0) |
6965 |
if (--ScanLimit == 0) |
| 6966 |
return false; |
6966 |
return false; |
| 6967 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
6967 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
| 6968 |
return false; |
6968 |
return false; |
| 6969 |
} |
6969 |
} |
| 6970 |
return true; |
6970 |
return true; |
| 6971 |
} |
6971 |
} |
| 6972 |
|
6972 |
|
| 6973 |
bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I, |
6973 |
bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I, |
| 6974 |
const Loop *L) { |
6974 |
const Loop *L) { |
| 6975 |
// The loop header is guaranteed to be executed for every iteration. |
6975 |
// The loop header is guaranteed to be executed for every iteration. |
| 6976 |
// |
6976 |
// |
| 6977 |
// FIXME: Relax this constraint to cover all basic blocks that are |
6977 |
// FIXME: Relax this constraint to cover all basic blocks that are |
| 6978 |
// guaranteed to be executed at every iteration. |
6978 |
// guaranteed to be executed at every iteration. |
| 6979 |
if (I->getParent() != L->getHeader()) return false; |
6979 |
if (I->getParent() != L->getHeader()) return false; |
| 6980 |
|
6980 |
|
| 6981 |
for (const Instruction &LI : *L->getHeader()) { |
6981 |
for (const Instruction &LI : *L->getHeader()) { |
| 6982 |
if (&LI == I) return true; |
6982 |
if (&LI == I) return true; |
| 6983 |
if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false; |
6983 |
if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false; |
| 6984 |
} |
6984 |
} |
| 6985 |
llvm_unreachable("Instruction not contained in its own parent basic block."); |
6985 |
llvm_unreachable("Instruction not contained in its own parent basic block."); |
| 6986 |
} |
6986 |
} |
| 6987 |
|
6987 |
|
| 6988 |
bool llvm::propagatesPoison(const Use &PoisonOp) { |
6988 |
bool llvm::propagatesPoison(const Use &PoisonOp) { |
| 6989 |
const Operator *I = cast(PoisonOp.getUser()); |
6989 |
const Operator *I = cast(PoisonOp.getUser()); |
| 6990 |
switch (I->getOpcode()) { |
6990 |
switch (I->getOpcode()) { |
| 6991 |
case Instruction::Freeze: |
6991 |
case Instruction::Freeze: |
| 6992 |
case Instruction::PHI: |
6992 |
case Instruction::PHI: |
| 6993 |
case Instruction::Invoke: |
6993 |
case Instruction::Invoke: |
| 6994 |
return false; |
6994 |
return false; |
| 6995 |
case Instruction::Select: |
6995 |
case Instruction::Select: |
| 6996 |
return PoisonOp.getOperandNo() == 0; |
6996 |
return PoisonOp.getOperandNo() == 0; |
| 6997 |
case Instruction::Call: |
6997 |
case Instruction::Call: |
| 6998 |
if (auto *II = dyn_cast(I)) { |
6998 |
if (auto *II = dyn_cast(I)) { |
| 6999 |
switch (II->getIntrinsicID()) { |
6999 |
switch (II->getIntrinsicID()) { |
| 7000 |
// TODO: Add more intrinsics. |
7000 |
// TODO: Add more intrinsics. |
| 7001 |
case Intrinsic::sadd_with_overflow: |
7001 |
case Intrinsic::sadd_with_overflow: |
| 7002 |
case Intrinsic::ssub_with_overflow: |
7002 |
case Intrinsic::ssub_with_overflow: |
| 7003 |
case Intrinsic::smul_with_overflow: |
7003 |
case Intrinsic::smul_with_overflow: |
| 7004 |
case Intrinsic::uadd_with_overflow: |
7004 |
case Intrinsic::uadd_with_overflow: |
| 7005 |
case Intrinsic::usub_with_overflow: |
7005 |
case Intrinsic::usub_with_overflow: |
| 7006 |
case Intrinsic::umul_with_overflow: |
7006 |
case Intrinsic::umul_with_overflow: |
| 7007 |
// If an input is a vector containing a poison element, the |
7007 |
// If an input is a vector containing a poison element, the |
| 7008 |
// two output vectors (calculated results, overflow bits)' |
7008 |
// two output vectors (calculated results, overflow bits)' |
| 7009 |
// corresponding lanes are poison. |
7009 |
// corresponding lanes are poison. |
| 7010 |
return true; |
7010 |
return true; |
| 7011 |
case Intrinsic::ctpop: |
7011 |
case Intrinsic::ctpop: |
| 7012 |
return true; |
7012 |
return true; |
| 7013 |
} |
7013 |
} |
| 7014 |
} |
7014 |
} |
| 7015 |
return false; |
7015 |
return false; |
| 7016 |
case Instruction::ICmp: |
7016 |
case Instruction::ICmp: |
| 7017 |
case Instruction::FCmp: |
7017 |
case Instruction::FCmp: |
| 7018 |
case Instruction::GetElementPtr: |
7018 |
case Instruction::GetElementPtr: |
| 7019 |
return true; |
7019 |
return true; |
| 7020 |
default: |
7020 |
default: |
| 7021 |
if (isa(I) || isa(I) || isa(I)) |
7021 |
if (isa(I) || isa(I) || isa(I)) |
| 7022 |
return true; |
7022 |
return true; |
| 7023 |
|
7023 |
|
| 7024 |
// Be conservative and return false. |
7024 |
// Be conservative and return false. |
| 7025 |
return false; |
7025 |
return false; |
| 7026 |
} |
7026 |
} |
| 7027 |
} |
7027 |
} |
| 7028 |
|
7028 |
|
| 7029 |
void llvm::getGuaranteedWellDefinedOps( |
7029 |
void llvm::getGuaranteedWellDefinedOps( |
| 7030 |
const Instruction *I, SmallVectorImpl &Operands) { |
7030 |
const Instruction *I, SmallVectorImpl &Operands) { |
| 7031 |
switch (I->getOpcode()) { |
7031 |
switch (I->getOpcode()) { |
| 7032 |
case Instruction::Store: |
7032 |
case Instruction::Store: |
| 7033 |
Operands.push_back(cast(I)->getPointerOperand()); |
7033 |
Operands.push_back(cast(I)->getPointerOperand()); |
| 7034 |
break; |
7034 |
break; |
| 7035 |
|
7035 |
|
| 7036 |
case Instruction::Load: |
7036 |
case Instruction::Load: |
| 7037 |
Operands.push_back(cast(I)->getPointerOperand()); |
7037 |
Operands.push_back(cast(I)->getPointerOperand()); |
| 7038 |
break; |
7038 |
break; |
| 7039 |
|
7039 |
|
| 7040 |
// Since dereferenceable attribute imply noundef, atomic operations |
7040 |
// Since dereferenceable attribute imply noundef, atomic operations |
| 7041 |
// also implicitly have noundef pointers too |
7041 |
// also implicitly have noundef pointers too |
| 7042 |
case Instruction::AtomicCmpXchg: |
7042 |
case Instruction::AtomicCmpXchg: |
| 7043 |
Operands.push_back(cast(I)->getPointerOperand()); |
7043 |
Operands.push_back(cast(I)->getPointerOperand()); |
| 7044 |
break; |
7044 |
break; |
| 7045 |
|
7045 |
|
| 7046 |
case Instruction::AtomicRMW: |
7046 |
case Instruction::AtomicRMW: |
| 7047 |
Operands.push_back(cast(I)->getPointerOperand()); |
7047 |
Operands.push_back(cast(I)->getPointerOperand()); |
| 7048 |
break; |
7048 |
break; |
| 7049 |
|
7049 |
|
| 7050 |
case Instruction::Call: |
7050 |
case Instruction::Call: |
| 7051 |
case Instruction::Invoke: { |
7051 |
case Instruction::Invoke: { |
| 7052 |
const CallBase *CB = cast(I); |
7052 |
const CallBase *CB = cast(I); |
| 7053 |
if (CB->isIndirectCall()) |
7053 |
if (CB->isIndirectCall()) |
| 7054 |
Operands.push_back(CB->getCalledOperand()); |
7054 |
Operands.push_back(CB->getCalledOperand()); |
| 7055 |
for (unsigned i = 0; i < CB->arg_size(); ++i) { |
7055 |
for (unsigned i = 0; i < CB->arg_size(); ++i) { |
| 7056 |
if (CB->paramHasAttr(i, Attribute::NoUndef) || |
7056 |
if (CB->paramHasAttr(i, Attribute::NoUndef) || |
| 7057 |
CB->paramHasAttr(i, Attribute::Dereferenceable) || |
7057 |
CB->paramHasAttr(i, Attribute::Dereferenceable) || |
| 7058 |
CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) |
7058 |
CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) |
| 7059 |
Operands.push_back(CB->getArgOperand(i)); |
7059 |
Operands.push_back(CB->getArgOperand(i)); |
| 7060 |
} |
7060 |
} |
| 7061 |
break; |
7061 |
break; |
| 7062 |
} |
7062 |
} |
| 7063 |
case Instruction::Ret: |
7063 |
case Instruction::Ret: |
| 7064 |
if (I->getFunction()->hasRetAttribute(Attribute::NoUndef)) |
7064 |
if (I->getFunction()->hasRetAttribute(Attribute::NoUndef)) |
| 7065 |
Operands.push_back(I->getOperand(0)); |
7065 |
Operands.push_back(I->getOperand(0)); |
| 7066 |
break; |
7066 |
break; |
| 7067 |
case Instruction::Switch: |
7067 |
case Instruction::Switch: |
| 7068 |
Operands.push_back(cast(I)->getCondition()); |
7068 |
Operands.push_back(cast(I)->getCondition()); |
| 7069 |
break; |
7069 |
break; |
| 7070 |
case Instruction::Br: { |
7070 |
case Instruction::Br: { |
| 7071 |
auto *BR = cast(I); |
7071 |
auto *BR = cast(I); |
| 7072 |
if (BR->isConditional()) |
7072 |
if (BR->isConditional()) |
| 7073 |
Operands.push_back(BR->getCondition()); |
7073 |
Operands.push_back(BR->getCondition()); |
| 7074 |
break; |
7074 |
break; |
| 7075 |
} |
7075 |
} |
| 7076 |
default: |
7076 |
default: |
| 7077 |
break; |
7077 |
break; |
| 7078 |
} |
7078 |
} |
| 7079 |
} |
7079 |
} |
| 7080 |
|
7080 |
|
| 7081 |
void llvm::getGuaranteedNonPoisonOps(const Instruction *I, |
7081 |
void llvm::getGuaranteedNonPoisonOps(const Instruction *I, |
| 7082 |
SmallVectorImpl &Operands) { |
7082 |
SmallVectorImpl &Operands) { |
| 7083 |
getGuaranteedWellDefinedOps(I, Operands); |
7083 |
getGuaranteedWellDefinedOps(I, Operands); |
| 7084 |
switch (I->getOpcode()) { |
7084 |
switch (I->getOpcode()) { |
| 7085 |
// Divisors of these operations are allowed to be partially undef. |
7085 |
// Divisors of these operations are allowed to be partially undef. |
| 7086 |
case Instruction::UDiv: |
7086 |
case Instruction::UDiv: |
| 7087 |
case Instruction::SDiv: |
7087 |
case Instruction::SDiv: |
| 7088 |
case Instruction::URem: |
7088 |
case Instruction::URem: |
| 7089 |
case Instruction::SRem: |
7089 |
case Instruction::SRem: |
| 7090 |
Operands.push_back(I->getOperand(1)); |
7090 |
Operands.push_back(I->getOperand(1)); |
| 7091 |
break; |
7091 |
break; |
| 7092 |
default: |
7092 |
default: |
| 7093 |
break; |
7093 |
break; |
| 7094 |
} |
7094 |
} |
| 7095 |
} |
7095 |
} |
| 7096 |
|
7096 |
|
| 7097 |
bool llvm::mustTriggerUB(const Instruction *I, |
7097 |
bool llvm::mustTriggerUB(const Instruction *I, |
| 7098 |
const SmallPtrSetImpl &KnownPoison) { |
7098 |
const SmallPtrSetImpl &KnownPoison) { |
| 7099 |
SmallVector NonPoisonOps; |
7099 |
SmallVector NonPoisonOps; |
| 7100 |
getGuaranteedNonPoisonOps(I, NonPoisonOps); |
7100 |
getGuaranteedNonPoisonOps(I, NonPoisonOps); |
| 7101 |
|
7101 |
|
| 7102 |
for (const auto *V : NonPoisonOps) |
7102 |
for (const auto *V : NonPoisonOps) |
| 7103 |
if (KnownPoison.count(V)) |
7103 |
if (KnownPoison.count(V)) |
| 7104 |
return true; |
7104 |
return true; |
| 7105 |
|
7105 |
|
| 7106 |
return false; |
7106 |
return false; |
| 7107 |
} |
7107 |
} |
| 7108 |
|
7108 |
|
| 7109 |
static bool programUndefinedIfUndefOrPoison(const Value *V, |
7109 |
static bool programUndefinedIfUndefOrPoison(const Value *V, |
| 7110 |
bool PoisonOnly) { |
7110 |
bool PoisonOnly) { |
| 7111 |
// We currently only look for uses of values within the same basic |
7111 |
// We currently only look for uses of values within the same basic |
| 7112 |
// block, as that makes it easier to guarantee that the uses will be |
7112 |
// block, as that makes it easier to guarantee that the uses will be |
| 7113 |
// executed given that Inst is executed. |
7113 |
// executed given that Inst is executed. |
| 7114 |
// |
7114 |
// |
| 7115 |
// FIXME: Expand this to consider uses beyond the same basic block. To do |
7115 |
// FIXME: Expand this to consider uses beyond the same basic block. To do |
| 7116 |
// this, look out for the distinction between post-dominance and strong |
7116 |
// this, look out for the distinction between post-dominance and strong |
| 7117 |
// post-dominance. |
7117 |
// post-dominance. |
| 7118 |
const BasicBlock *BB = nullptr; |
7118 |
const BasicBlock *BB = nullptr; |
| 7119 |
BasicBlock::const_iterator Begin; |
7119 |
BasicBlock::const_iterator Begin; |
| 7120 |
if (const auto *Inst = dyn_cast(V)) { |
7120 |
if (const auto *Inst = dyn_cast(V)) { |
| 7121 |
BB = Inst->getParent(); |
7121 |
BB = Inst->getParent(); |
| 7122 |
Begin = Inst->getIterator(); |
7122 |
Begin = Inst->getIterator(); |
| 7123 |
Begin++; |
7123 |
Begin++; |
| 7124 |
} else if (const auto *Arg = dyn_cast(V)) { |
7124 |
} else if (const auto *Arg = dyn_cast(V)) { |
| 7125 |
BB = &Arg->getParent()->getEntryBlock(); |
7125 |
BB = &Arg->getParent()->getEntryBlock(); |
| 7126 |
Begin = BB->begin(); |
7126 |
Begin = BB->begin(); |
| 7127 |
} else { |
7127 |
} else { |
| 7128 |
return false; |
7128 |
return false; |
| 7129 |
} |
7129 |
} |
| 7130 |
|
7130 |
|
| 7131 |
// Limit number of instructions we look at, to avoid scanning through large |
7131 |
// Limit number of instructions we look at, to avoid scanning through large |
| 7132 |
// blocks. The current limit is chosen arbitrarily. |
7132 |
// blocks. The current limit is chosen arbitrarily. |
| 7133 |
unsigned ScanLimit = 32; |
7133 |
unsigned ScanLimit = 32; |
| 7134 |
BasicBlock::const_iterator End = BB->end(); |
7134 |
BasicBlock::const_iterator End = BB->end(); |
| 7135 |
|
7135 |
|
| 7136 |
if (!PoisonOnly) { |
7136 |
if (!PoisonOnly) { |
| 7137 |
// Since undef does not propagate eagerly, be conservative & just check |
7137 |
// Since undef does not propagate eagerly, be conservative & just check |
| 7138 |
// whether a value is directly passed to an instruction that must take |
7138 |
// whether a value is directly passed to an instruction that must take |
| 7139 |
// well-defined operands. |
7139 |
// well-defined operands. |
| 7140 |
|
7140 |
|
| 7141 |
for (const auto &I : make_range(Begin, End)) { |
7141 |
for (const auto &I : make_range(Begin, End)) { |
| 7142 |
if (isa(I)) |
7142 |
if (isa(I)) |
| 7143 |
continue; |
7143 |
continue; |
| 7144 |
if (--ScanLimit == 0) |
7144 |
if (--ScanLimit == 0) |
| 7145 |
break; |
7145 |
break; |
| 7146 |
|
7146 |
|
| 7147 |
SmallVector WellDefinedOps; |
7147 |
SmallVector WellDefinedOps; |
| 7148 |
getGuaranteedWellDefinedOps(&I, WellDefinedOps); |
7148 |
getGuaranteedWellDefinedOps(&I, WellDefinedOps); |
| 7149 |
if (is_contained(WellDefinedOps, V)) |
7149 |
if (is_contained(WellDefinedOps, V)) |
| 7150 |
return true; |
7150 |
return true; |
| 7151 |
|
7151 |
|
| 7152 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
7152 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
| 7153 |
break; |
7153 |
break; |
| 7154 |
} |
7154 |
} |
| 7155 |
return false; |
7155 |
return false; |
| 7156 |
} |
7156 |
} |
| 7157 |
|
7157 |
|
| 7158 |
// Set of instructions that we have proved will yield poison if Inst |
7158 |
// Set of instructions that we have proved will yield poison if Inst |
| 7159 |
// does. |
7159 |
// does. |
| 7160 |
SmallSet YieldsPoison; |
7160 |
SmallSet YieldsPoison; |
| 7161 |
SmallSet Visited; |
7161 |
SmallSet Visited; |
| 7162 |
|
7162 |
|
| 7163 |
YieldsPoison.insert(V); |
7163 |
YieldsPoison.insert(V); |
| 7164 |
Visited.insert(BB); |
7164 |
Visited.insert(BB); |
| 7165 |
|
7165 |
|
| 7166 |
while (true) { |
7166 |
while (true) { |
| 7167 |
for (const auto &I : make_range(Begin, End)) { |
7167 |
for (const auto &I : make_range(Begin, End)) { |
| 7168 |
if (isa(I)) |
7168 |
if (isa(I)) |
| 7169 |
continue; |
7169 |
continue; |
| 7170 |
if (--ScanLimit == 0) |
7170 |
if (--ScanLimit == 0) |
| 7171 |
return false; |
7171 |
return false; |
| 7172 |
if (mustTriggerUB(&I, YieldsPoison)) |
7172 |
if (mustTriggerUB(&I, YieldsPoison)) |
| 7173 |
return true; |
7173 |
return true; |
| 7174 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
7174 |
if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
| 7175 |
return false; |
7175 |
return false; |
| 7176 |
|
7176 |
|
| 7177 |
// If an operand is poison and propagates it, mark I as yielding poison. |
7177 |
// If an operand is poison and propagates it, mark I as yielding poison. |
| 7178 |
for (const Use &Op : I.operands()) { |
7178 |
for (const Use &Op : I.operands()) { |
| 7179 |
if (YieldsPoison.count(Op) && propagatesPoison(Op)) { |
7179 |
if (YieldsPoison.count(Op) && propagatesPoison(Op)) { |
| 7180 |
YieldsPoison.insert(&I); |
7180 |
YieldsPoison.insert(&I); |
| 7181 |
break; |
7181 |
break; |
| 7182 |
} |
7182 |
} |
| 7183 |
} |
7183 |
} |
| 7184 |
|
7184 |
|
| 7185 |
// Special handling for select, which returns poison if its operand 0 is |
7185 |
// Special handling for select, which returns poison if its operand 0 is |
| 7186 |
// poison (handled in the loop above) *or* if both its true/false operands |
7186 |
// poison (handled in the loop above) *or* if both its true/false operands |
| 7187 |
// are poison (handled here). |
7187 |
// are poison (handled here). |
| 7188 |
if (I.getOpcode() == Instruction::Select && |
7188 |
if (I.getOpcode() == Instruction::Select && |
| 7189 |
YieldsPoison.count(I.getOperand(1)) && |
7189 |
YieldsPoison.count(I.getOperand(1)) && |
| 7190 |
YieldsPoison.count(I.getOperand(2))) { |
7190 |
YieldsPoison.count(I.getOperand(2))) { |
| 7191 |
YieldsPoison.insert(&I); |
7191 |
YieldsPoison.insert(&I); |
| 7192 |
} |
7192 |
} |
| 7193 |
} |
7193 |
} |
| 7194 |
|
7194 |
|
| 7195 |
BB = BB->getSingleSuccessor(); |
7195 |
BB = BB->getSingleSuccessor(); |
| 7196 |
if (!BB || !Visited.insert(BB).second) |
7196 |
if (!BB || !Visited.insert(BB).second) |
| 7197 |
break; |
7197 |
break; |
| 7198 |
|
7198 |
|
| 7199 |
Begin = BB->getFirstNonPHI()->getIterator(); |
7199 |
Begin = BB->getFirstNonPHI()->getIterator(); |
| 7200 |
End = BB->end(); |
7200 |
End = BB->end(); |
| 7201 |
} |
7201 |
} |
| 7202 |
return false; |
7202 |
return false; |
| 7203 |
} |
7203 |
} |
| 7204 |
|
7204 |
|
| 7205 |
bool llvm::programUndefinedIfUndefOrPoison(const Instruction *Inst) { |
7205 |
bool llvm::programUndefinedIfUndefOrPoison(const Instruction *Inst) { |
| 7206 |
return ::programUndefinedIfUndefOrPoison(Inst, false); |
7206 |
return ::programUndefinedIfUndefOrPoison(Inst, false); |
| 7207 |
} |
7207 |
} |
| 7208 |
|
7208 |
|
| 7209 |
bool llvm::programUndefinedIfPoison(const Instruction *Inst) { |
7209 |
bool llvm::programUndefinedIfPoison(const Instruction *Inst) { |
| 7210 |
return ::programUndefinedIfUndefOrPoison(Inst, true); |
7210 |
return ::programUndefinedIfUndefOrPoison(Inst, true); |
| 7211 |
} |
7211 |
} |
| 7212 |
|
7212 |
|
| 7213 |
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) { |
7213 |
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) { |
| 7214 |
if (FMF.noNaNs()) |
7214 |
if (FMF.noNaNs()) |
| 7215 |
return true; |
7215 |
return true; |
| 7216 |
|
7216 |
|
| 7217 |
if (auto *C = dyn_cast(V)) |
7217 |
if (auto *C = dyn_cast(V)) |
| 7218 |
return !C->isNaN(); |
7218 |
return !C->isNaN(); |
| 7219 |
|
7219 |
|
| 7220 |
if (auto *C = dyn_cast(V)) { |
7220 |
if (auto *C = dyn_cast(V)) { |
| 7221 |
if (!C->getElementType()->isFloatingPointTy()) |
7221 |
if (!C->getElementType()->isFloatingPointTy()) |
| 7222 |
return false; |
7222 |
return false; |
| 7223 |
for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { |
7223 |
for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { |
| 7224 |
if (C->getElementAsAPFloat(I).isNaN()) |
7224 |
if (C->getElementAsAPFloat(I).isNaN()) |
| 7225 |
return false; |
7225 |
return false; |
| 7226 |
} |
7226 |
} |
| 7227 |
return true; |
7227 |
return true; |
| 7228 |
} |
7228 |
} |
| 7229 |
|
7229 |
|
| 7230 |
if (isa(V)) |
7230 |
if (isa(V)) |
| 7231 |
return true; |
7231 |
return true; |
| 7232 |
|
7232 |
|
| 7233 |
return false; |
7233 |
return false; |
| 7234 |
} |
7234 |
} |
| 7235 |
|
7235 |
|
| 7236 |
static bool isKnownNonZero(const Value *V) { |
7236 |
static bool isKnownNonZero(const Value *V) { |
| 7237 |
if (auto *C = dyn_cast(V)) |
7237 |
if (auto *C = dyn_cast(V)) |
| 7238 |
return !C->isZero(); |
7238 |
return !C->isZero(); |
| 7239 |
|
7239 |
|
| 7240 |
if (auto *C = dyn_cast(V)) { |
7240 |
if (auto *C = dyn_cast(V)) { |
| 7241 |
if (!C->getElementType()->isFloatingPointTy()) |
7241 |
if (!C->getElementType()->isFloatingPointTy()) |
| 7242 |
return false; |
7242 |
return false; |
| 7243 |
for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { |
7243 |
for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { |
| 7244 |
if (C->getElementAsAPFloat(I).isZero()) |
7244 |
if (C->getElementAsAPFloat(I).isZero()) |
| 7245 |
return false; |
7245 |
return false; |
| 7246 |
} |
7246 |
} |
| 7247 |
return true; |
7247 |
return true; |
| 7248 |
} |
7248 |
} |
| 7249 |
|
7249 |
|
| 7250 |
return false; |
7250 |
return false; |
| 7251 |
} |
7251 |
} |
| 7252 |
|
7252 |
|
| 7253 |
/// Match clamp pattern for float types without care about NaNs or signed zeros. |
7253 |
/// Match clamp pattern for float types without care about NaNs or signed zeros. |
| 7254 |
/// Given non-min/max outer cmp/select from the clamp pattern this |
7254 |
/// Given non-min/max outer cmp/select from the clamp pattern this |
| 7255 |
/// function recognizes if it can be substitued by a "canonical" min/max |
7255 |
/// function recognizes if it can be substitued by a "canonical" min/max |
| 7256 |
/// pattern. |
7256 |
/// pattern. |
| 7257 |
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, |
7257 |
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, |
| 7258 |
Value *CmpLHS, Value *CmpRHS, |
7258 |
Value *CmpLHS, Value *CmpRHS, |
| 7259 |
Value *TrueVal, Value *FalseVal, |
7259 |
Value *TrueVal, Value *FalseVal, |
| 7260 |
Value *&LHS, Value *&RHS) { |
7260 |
Value *&LHS, Value *&RHS) { |
| 7261 |
// Try to match |
7261 |
// Try to match |
| 7262 |
// X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2)) |
7262 |
// X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2)) |
| 7263 |
// X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2)) |
7263 |
// X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2)) |
| 7264 |
// and return description of the outer Max/Min. |
7264 |
// and return description of the outer Max/Min. |
| 7265 |
|
7265 |
|
| 7266 |
// First, check if select has inverse order: |
7266 |
// First, check if select has inverse order: |
| 7267 |
if (CmpRHS == FalseVal) { |
7267 |
if (CmpRHS == FalseVal) { |
| 7268 |
std::swap(TrueVal, FalseVal); |
7268 |
std::swap(TrueVal, FalseVal); |
| 7269 |
Pred = CmpInst::getInversePredicate(Pred); |
7269 |
Pred = CmpInst::getInversePredicate(Pred); |
| 7270 |
} |
7270 |
} |
| 7271 |
|
7271 |
|
| 7272 |
// Assume success now. If there's no match, callers should not use these anyway. |
7272 |
// Assume success now. If there's no match, callers should not use these anyway. |
| 7273 |
LHS = TrueVal; |
7273 |
LHS = TrueVal; |
| 7274 |
RHS = FalseVal; |
7274 |
RHS = FalseVal; |
| 7275 |
|
7275 |
|
| 7276 |
const APFloat *FC1; |
7276 |
const APFloat *FC1; |
| 7277 |
if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite()) |
7277 |
if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite()) |
| 7278 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7278 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7279 |
|
7279 |
|
| 7280 |
const APFloat *FC2; |
7280 |
const APFloat *FC2; |
| 7281 |
switch (Pred) { |
7281 |
switch (Pred) { |
| 7282 |
case CmpInst::FCMP_OLT: |
7282 |
case CmpInst::FCMP_OLT: |
| 7283 |
case CmpInst::FCMP_OLE: |
7283 |
case CmpInst::FCMP_OLE: |
| 7284 |
case CmpInst::FCMP_ULT: |
7284 |
case CmpInst::FCMP_ULT: |
| 7285 |
case CmpInst::FCMP_ULE: |
7285 |
case CmpInst::FCMP_ULE: |
| 7286 |
if (match(FalseVal, |
7286 |
if (match(FalseVal, |
| 7287 |
m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)), |
7287 |
m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)), |
| 7288 |
m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) && |
7288 |
m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) && |
| 7289 |
*FC1 < *FC2) |
7289 |
*FC1 < *FC2) |
| 7290 |
return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false}; |
7290 |
return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false}; |
| 7291 |
break; |
7291 |
break; |
| 7292 |
case CmpInst::FCMP_OGT: |
7292 |
case CmpInst::FCMP_OGT: |
| 7293 |
case CmpInst::FCMP_OGE: |
7293 |
case CmpInst::FCMP_OGE: |
| 7294 |
case CmpInst::FCMP_UGT: |
7294 |
case CmpInst::FCMP_UGT: |
| 7295 |
case CmpInst::FCMP_UGE: |
7295 |
case CmpInst::FCMP_UGE: |
| 7296 |
if (match(FalseVal, |
7296 |
if (match(FalseVal, |
| 7297 |
m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)), |
7297 |
m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)), |
| 7298 |
m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) && |
7298 |
m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) && |
| 7299 |
*FC1 > *FC2) |
7299 |
*FC1 > *FC2) |
| 7300 |
return {SPF_FMINNUM, SPNB_RETURNS_ANY, false}; |
7300 |
return {SPF_FMINNUM, SPNB_RETURNS_ANY, false}; |
| 7301 |
break; |
7301 |
break; |
| 7302 |
default: |
7302 |
default: |
| 7303 |
break; |
7303 |
break; |
| 7304 |
} |
7304 |
} |
| 7305 |
|
7305 |
|
| 7306 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7306 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7307 |
} |
7307 |
} |
| 7308 |
|
7308 |
|
| 7309 |
/// Recognize variations of: |
7309 |
/// Recognize variations of: |
| 7310 |
/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v))) |
7310 |
/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v))) |
| 7311 |
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, |
7311 |
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, |
| 7312 |
Value *CmpLHS, Value *CmpRHS, |
7312 |
Value *CmpLHS, Value *CmpRHS, |
| 7313 |
Value *TrueVal, Value *FalseVal) { |
7313 |
Value *TrueVal, Value *FalseVal) { |
| 7314 |
// Swap the select operands and predicate to match the patterns below. |
7314 |
// Swap the select operands and predicate to match the patterns below. |
| 7315 |
if (CmpRHS != TrueVal) { |
7315 |
if (CmpRHS != TrueVal) { |
| 7316 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
7316 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
| 7317 |
std::swap(TrueVal, FalseVal); |
7317 |
std::swap(TrueVal, FalseVal); |
| 7318 |
} |
7318 |
} |
| 7319 |
const APInt *C1; |
7319 |
const APInt *C1; |
| 7320 |
if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) { |
7320 |
if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) { |
| 7321 |
const APInt *C2; |
7321 |
const APInt *C2; |
| 7322 |
// (X SMAX(SMIN(X, C2), C1) |
7322 |
// (X SMAX(SMIN(X, C2), C1) |
| 7323 |
if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) && |
7323 |
if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) && |
| 7324 |
C1->slt(*C2) && Pred == CmpInst::ICMP_SLT) |
7324 |
C1->slt(*C2) && Pred == CmpInst::ICMP_SLT) |
| 7325 |
return {SPF_SMAX, SPNB_NA, false}; |
7325 |
return {SPF_SMAX, SPNB_NA, false}; |
| 7326 |
|
7326 |
|
| 7327 |
// (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) |
7327 |
// (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) |
| 7328 |
if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) && |
7328 |
if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) && |
| 7329 |
C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT) |
7329 |
C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT) |
| 7330 |
return {SPF_SMIN, SPNB_NA, false}; |
7330 |
return {SPF_SMIN, SPNB_NA, false}; |
| 7331 |
|
7331 |
|
| 7332 |
// (X UMAX(UMIN(X, C2), C1) |
7332 |
// (X UMAX(UMIN(X, C2), C1) |
| 7333 |
if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) && |
7333 |
if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) && |
| 7334 |
C1->ult(*C2) && Pred == CmpInst::ICMP_ULT) |
7334 |
C1->ult(*C2) && Pred == CmpInst::ICMP_ULT) |
| 7335 |
return {SPF_UMAX, SPNB_NA, false}; |
7335 |
return {SPF_UMAX, SPNB_NA, false}; |
| 7336 |
|
7336 |
|
| 7337 |
// (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) |
7337 |
// (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) |
| 7338 |
if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) && |
7338 |
if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) && |
| 7339 |
C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT) |
7339 |
C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT) |
| 7340 |
return {SPF_UMIN, SPNB_NA, false}; |
7340 |
return {SPF_UMIN, SPNB_NA, false}; |
| 7341 |
} |
7341 |
} |
| 7342 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7342 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7343 |
} |
7343 |
} |
| 7344 |
|
7344 |
|
| 7345 |
/// Recognize variations of: |
7345 |
/// Recognize variations of: |
| 7346 |
/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c)) |
7346 |
/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c)) |
| 7347 |
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, |
7347 |
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, |
| 7348 |
Value *CmpLHS, Value *CmpRHS, |
7348 |
Value *CmpLHS, Value *CmpRHS, |
| 7349 |
Value *TVal, Value *FVal, |
7349 |
Value *TVal, Value *FVal, |
| 7350 |
unsigned Depth) { |
7350 |
unsigned Depth) { |
| 7351 |
// TODO: Allow FP min/max with nnan/nsz. |
7351 |
// TODO: Allow FP min/max with nnan/nsz. |
| 7352 |
assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison"); |
7352 |
assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison"); |
| 7353 |
|
7353 |
|
| 7354 |
Value *A = nullptr, *B = nullptr; |
7354 |
Value *A = nullptr, *B = nullptr; |
| 7355 |
SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1); |
7355 |
SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1); |
| 7356 |
if (!SelectPatternResult::isMinOrMax(L.Flavor)) |
7356 |
if (!SelectPatternResult::isMinOrMax(L.Flavor)) |
| 7357 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7357 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7358 |
|
7358 |
|
| 7359 |
Value *C = nullptr, *D = nullptr; |
7359 |
Value *C = nullptr, *D = nullptr; |
| 7360 |
SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1); |
7360 |
SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1); |
| 7361 |
if (L.Flavor != R.Flavor) |
7361 |
if (L.Flavor != R.Flavor) |
| 7362 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7362 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7363 |
|
7363 |
|
| 7364 |
// We have something like: x Pred y ? min(a, b) : min(c, d). |
7364 |
// We have something like: x Pred y ? min(a, b) : min(c, d). |
| 7365 |
// Try to match the compare to the min/max operations of the select operands. |
7365 |
// Try to match the compare to the min/max operations of the select operands. |
| 7366 |
// First, make sure we have the right compare predicate. |
7366 |
// First, make sure we have the right compare predicate. |
| 7367 |
switch (L.Flavor) { |
7367 |
switch (L.Flavor) { |
| 7368 |
case SPF_SMIN: |
7368 |
case SPF_SMIN: |
| 7369 |
if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) { |
7369 |
if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) { |
| 7370 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
7370 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
| 7371 |
std::swap(CmpLHS, CmpRHS); |
7371 |
std::swap(CmpLHS, CmpRHS); |
| 7372 |
} |
7372 |
} |
| 7373 |
if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
7373 |
if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
| 7374 |
break; |
7374 |
break; |
| 7375 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7375 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7376 |
case SPF_SMAX: |
7376 |
case SPF_SMAX: |
| 7377 |
if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) { |
7377 |
if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) { |
| 7378 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
7378 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
| 7379 |
std::swap(CmpLHS, CmpRHS); |
7379 |
std::swap(CmpLHS, CmpRHS); |
| 7380 |
} |
7380 |
} |
| 7381 |
if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) |
7381 |
if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) |
| 7382 |
break; |
7382 |
break; |
| 7383 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7383 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7384 |
case SPF_UMIN: |
7384 |
case SPF_UMIN: |
| 7385 |
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) { |
7385 |
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) { |
| 7386 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
7386 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
| 7387 |
std::swap(CmpLHS, CmpRHS); |
7387 |
std::swap(CmpLHS, CmpRHS); |
| 7388 |
} |
7388 |
} |
| 7389 |
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) |
7389 |
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) |
| 7390 |
break; |
7390 |
break; |
| 7391 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7391 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7392 |
case SPF_UMAX: |
7392 |
case SPF_UMAX: |
| 7393 |
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { |
7393 |
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { |
| 7394 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
7394 |
Pred = ICmpInst::getSwappedPredicate(Pred); |
| 7395 |
std::swap(CmpLHS, CmpRHS); |
7395 |
std::swap(CmpLHS, CmpRHS); |
| 7396 |
} |
7396 |
} |
| 7397 |
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) |
7397 |
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) |
| 7398 |
break; |
7398 |
break; |
| 7399 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7399 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7400 |
default: |
7400 |
default: |
| 7401 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7401 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7402 |
} |
7402 |
} |
| 7403 |
|
7403 |
|
| 7404 |
// If there is a common operand in the already matched min/max and the other |
7404 |
// If there is a common operand in the already matched min/max and the other |
| 7405 |
// min/max operands match the compare operands (either directly or inverted), |
7405 |
// min/max operands match the compare operands (either directly or inverted), |
| 7406 |
// then this is min/max of the same flavor. |
7406 |
// then this is min/max of the same flavor. |
| 7407 |
|
7407 |
|
| 7408 |
// a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) |
7408 |
// a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) |
| 7409 |
// ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) |
7409 |
// ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) |
| 7410 |
if (D == B) { |
7410 |
if (D == B) { |
| 7411 |
if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && |
7411 |
if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && |
| 7412 |
match(A, m_Not(m_Specific(CmpRHS))))) |
7412 |
match(A, m_Not(m_Specific(CmpRHS))))) |
| 7413 |
return {L.Flavor, SPNB_NA, false}; |
7413 |
return {L.Flavor, SPNB_NA, false}; |
| 7414 |
} |
7414 |
} |
| 7415 |
// a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) |
7415 |
// a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) |
| 7416 |
// ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) |
7416 |
// ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) |
| 7417 |
if (C == B) { |
7417 |
if (C == B) { |
| 7418 |
if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && |
7418 |
if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && |
| 7419 |
match(A, m_Not(m_Specific(CmpRHS))))) |
7419 |
match(A, m_Not(m_Specific(CmpRHS))))) |
| 7420 |
return {L.Flavor, SPNB_NA, false}; |
7420 |
return {L.Flavor, SPNB_NA, false}; |
| 7421 |
} |
7421 |
} |
| 7422 |
// b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) |
7422 |
// b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) |
| 7423 |
// ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) |
7423 |
// ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) |
| 7424 |
if (D == A) { |
7424 |
if (D == A) { |
| 7425 |
if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && |
7425 |
if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && |
| 7426 |
match(B, m_Not(m_Specific(CmpRHS))))) |
7426 |
match(B, m_Not(m_Specific(CmpRHS))))) |
| 7427 |
return {L.Flavor, SPNB_NA, false}; |
7427 |
return {L.Flavor, SPNB_NA, false}; |
| 7428 |
} |
7428 |
} |
| 7429 |
// b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) |
7429 |
// b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) |
| 7430 |
// ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) |
7430 |
// ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) |
| 7431 |
if (C == A) { |
7431 |
if (C == A) { |
| 7432 |
if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && |
7432 |
if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && |
| 7433 |
match(B, m_Not(m_Specific(CmpRHS))))) |
7433 |
match(B, m_Not(m_Specific(CmpRHS))))) |
| 7434 |
return {L.Flavor, SPNB_NA, false}; |
7434 |
return {L.Flavor, SPNB_NA, false}; |
| 7435 |
} |
7435 |
} |
| 7436 |
|
7436 |
|
| 7437 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7437 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7438 |
} |
7438 |
} |
| 7439 |
|
7439 |
|
| 7440 |
/// If the input value is the result of a 'not' op, constant integer, or vector |
7440 |
/// If the input value is the result of a 'not' op, constant integer, or vector |
| 7441 |
/// splat of a constant integer, return the bitwise-not source value. |
7441 |
/// splat of a constant integer, return the bitwise-not source value. |
| 7442 |
/// TODO: This could be extended to handle non-splat vector integer constants. |
7442 |
/// TODO: This could be extended to handle non-splat vector integer constants. |
| 7443 |
static Value *getNotValue(Value *V) { |
7443 |
static Value *getNotValue(Value *V) { |
| 7444 |
Value *NotV; |
7444 |
Value *NotV; |
| 7445 |
if (match(V, m_Not(m_Value(NotV)))) |
7445 |
if (match(V, m_Not(m_Value(NotV)))) |
| 7446 |
return NotV; |
7446 |
return NotV; |
| 7447 |
|
7447 |
|
| 7448 |
const APInt *C; |
7448 |
const APInt *C; |
| 7449 |
if (match(V, m_APInt(C))) |
7449 |
if (match(V, m_APInt(C))) |
| 7450 |
return ConstantInt::get(V->getType(), ~(*C)); |
7450 |
return ConstantInt::get(V->getType(), ~(*C)); |
| 7451 |
|
7451 |
|
| 7452 |
return nullptr; |
7452 |
return nullptr; |
| 7453 |
} |
7453 |
} |
| 7454 |
|
7454 |
|
| 7455 |
/// Match non-obvious integer minimum and maximum sequences. |
7455 |
/// Match non-obvious integer minimum and maximum sequences. |
| 7456 |
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, |
7456 |
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, |
| 7457 |
Value *CmpLHS, Value *CmpRHS, |
7457 |
Value *CmpLHS, Value *CmpRHS, |
| 7458 |
Value *TrueVal, Value *FalseVal, |
7458 |
Value *TrueVal, Value *FalseVal, |
| 7459 |
Value *&LHS, Value *&RHS, |
7459 |
Value *&LHS, Value *&RHS, |
| 7460 |
unsigned Depth) { |
7460 |
unsigned Depth) { |
| 7461 |
// Assume success. If there's no match, callers should not use these anyway. |
7461 |
// Assume success. If there's no match, callers should not use these anyway. |
| 7462 |
LHS = TrueVal; |
7462 |
LHS = TrueVal; |
| 7463 |
RHS = FalseVal; |
7463 |
RHS = FalseVal; |
| 7464 |
|
7464 |
|
| 7465 |
SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal); |
7465 |
SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal); |
| 7466 |
if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) |
7466 |
if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) |
| 7467 |
return SPR; |
7467 |
return SPR; |
| 7468 |
|
7468 |
|
| 7469 |
SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth); |
7469 |
SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth); |
| 7470 |
if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) |
7470 |
if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) |
| 7471 |
return SPR; |
7471 |
return SPR; |
| 7472 |
|
7472 |
|
| 7473 |
// Look through 'not' ops to find disguised min/max. |
7473 |
// Look through 'not' ops to find disguised min/max. |
| 7474 |
// (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y) |
7474 |
// (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y) |
| 7475 |
// (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y) |
7475 |
// (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y) |
| 7476 |
if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) { |
7476 |
if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) { |
| 7477 |
switch (Pred) { |
7477 |
switch (Pred) { |
| 7478 |
case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false}; |
7478 |
case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false}; |
| 7479 |
case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false}; |
7479 |
case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false}; |
| 7480 |
case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false}; |
7480 |
case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false}; |
| 7481 |
case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false}; |
7481 |
case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false}; |
| 7482 |
default: break; |
7482 |
default: break; |
| 7483 |
} |
7483 |
} |
| 7484 |
} |
7484 |
} |
| 7485 |
|
7485 |
|
| 7486 |
// (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X) |
7486 |
// (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X) |
| 7487 |
// (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X) |
7487 |
// (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X) |
| 7488 |
if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) { |
7488 |
if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) { |
| 7489 |
switch (Pred) { |
7489 |
switch (Pred) { |
| 7490 |
case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false}; |
7490 |
case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false}; |
| 7491 |
case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false}; |
7491 |
case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false}; |
| 7492 |
case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false}; |
7492 |
case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false}; |
| 7493 |
case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false}; |
7493 |
case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false}; |
| 7494 |
default: break; |
7494 |
default: break; |
| 7495 |
} |
7495 |
} |
| 7496 |
} |
7496 |
} |
| 7497 |
|
7497 |
|
| 7498 |
if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT) |
7498 |
if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT) |
| 7499 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7499 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7500 |
|
7500 |
|
| 7501 |
const APInt *C1; |
7501 |
const APInt *C1; |
| 7502 |
if (!match(CmpRHS, m_APInt(C1))) |
7502 |
if (!match(CmpRHS, m_APInt(C1))) |
| 7503 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7503 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7504 |
|
7504 |
|
| 7505 |
// An unsigned min/max can be written with a signed compare. |
7505 |
// An unsigned min/max can be written with a signed compare. |
| 7506 |
const APInt *C2; |
7506 |
const APInt *C2; |
| 7507 |
if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) || |
7507 |
if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) || |
| 7508 |
(CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) { |
7508 |
(CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) { |
| 7509 |
// Is the sign bit set? |
7509 |
// Is the sign bit set? |
| 7510 |
// (X (X >u MAXVAL) ? X : MAXVAL ==> UMAX |
7510 |
// (X (X >u MAXVAL) ? X : MAXVAL ==> UMAX |
| 7511 |
// (X (X >u MAXVAL) ? MAXVAL : X ==> UMIN |
7511 |
// (X (X >u MAXVAL) ? MAXVAL : X ==> UMIN |
| 7512 |
if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue()) |
7512 |
if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue()) |
| 7513 |
return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; |
7513 |
return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; |
| 7514 |
|
7514 |
|
| 7515 |
// Is the sign bit clear? |
7515 |
// Is the sign bit clear? |
| 7516 |
// (X >s -1) ? MINVAL : X ==> (X UMAX |
7516 |
// (X >s -1) ? MINVAL : X ==> (X UMAX |
| 7517 |
// (X >s -1) ? X : MINVAL ==> (X UMIN |
7517 |
// (X >s -1) ? X : MINVAL ==> (X UMIN |
| 7518 |
if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue()) |
7518 |
if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue()) |
| 7519 |
return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; |
7519 |
return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; |
| 7520 |
} |
7520 |
} |
| 7521 |
|
7521 |
|
| 7522 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7522 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7523 |
} |
7523 |
} |
| 7524 |
|
7524 |
|
| 7525 |
bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) { |
7525 |
bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) { |
| 7526 |
assert(X && Y && "Invalid operand"); |
7526 |
assert(X && Y && "Invalid operand"); |
| 7527 |
|
7527 |
|
| 7528 |
// X = sub (0, Y) || X = sub nsw (0, Y) |
7528 |
// X = sub (0, Y) || X = sub nsw (0, Y) |
| 7529 |
if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) || |
7529 |
if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) || |
| 7530 |
(NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y))))) |
7530 |
(NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y))))) |
| 7531 |
return true; |
7531 |
return true; |
| 7532 |
|
7532 |
|
| 7533 |
// Y = sub (0, X) || Y = sub nsw (0, X) |
7533 |
// Y = sub (0, X) || Y = sub nsw (0, X) |
| 7534 |
if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) || |
7534 |
if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) || |
| 7535 |
(NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X))))) |
7535 |
(NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X))))) |
| 7536 |
return true; |
7536 |
return true; |
| 7537 |
|
7537 |
|
| 7538 |
// X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) |
7538 |
// X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) |
| 7539 |
Value *A, *B; |
7539 |
Value *A, *B; |
| 7540 |
return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) && |
7540 |
return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) && |
| 7541 |
match(Y, m_Sub(m_Specific(B), m_Specific(A))))) || |
7541 |
match(Y, m_Sub(m_Specific(B), m_Specific(A))))) || |
| 7542 |
(NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) && |
7542 |
(NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) && |
| 7543 |
match(Y, m_NSWSub(m_Specific(B), m_Specific(A))))); |
7543 |
match(Y, m_NSWSub(m_Specific(B), m_Specific(A))))); |
| 7544 |
} |
7544 |
} |
| 7545 |
|
7545 |
|
| 7546 |
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, |
7546 |
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, |
| 7547 |
FastMathFlags FMF, |
7547 |
FastMathFlags FMF, |
| 7548 |
Value *CmpLHS, Value *CmpRHS, |
7548 |
Value *CmpLHS, Value *CmpRHS, |
| 7549 |
Value *TrueVal, Value *FalseVal, |
7549 |
Value *TrueVal, Value *FalseVal, |
| 7550 |
Value *&LHS, Value *&RHS, |
7550 |
Value *&LHS, Value *&RHS, |
| 7551 |
unsigned Depth) { |
7551 |
unsigned Depth) { |
| 7552 |
bool HasMismatchedZeros = false; |
7552 |
bool HasMismatchedZeros = false; |
| 7553 |
if (CmpInst::isFPPredicate(Pred)) { |
7553 |
if (CmpInst::isFPPredicate(Pred)) { |
| 7554 |
// IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one |
7554 |
// IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one |
| 7555 |
// 0.0 operand, set the compare's 0.0 operands to that same value for the |
7555 |
// 0.0 operand, set the compare's 0.0 operands to that same value for the |
| 7556 |
// purpose of identifying min/max. Disregard vector constants with undefined |
7556 |
// purpose of identifying min/max. Disregard vector constants with undefined |
| 7557 |
// elements because those can not be back-propagated for analysis. |
7557 |
// elements because those can not be back-propagated for analysis. |
| 7558 |
Value *OutputZeroVal = nullptr; |
7558 |
Value *OutputZeroVal = nullptr; |
| 7559 |
if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) && |
7559 |
if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) && |
| 7560 |
!cast(TrueVal)->containsUndefOrPoisonElement()) |
7560 |
!cast(TrueVal)->containsUndefOrPoisonElement()) |
| 7561 |
OutputZeroVal = TrueVal; |
7561 |
OutputZeroVal = TrueVal; |
| 7562 |
else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) && |
7562 |
else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) && |
| 7563 |
!cast(FalseVal)->containsUndefOrPoisonElement()) |
7563 |
!cast(FalseVal)->containsUndefOrPoisonElement()) |
| 7564 |
OutputZeroVal = FalseVal; |
7564 |
OutputZeroVal = FalseVal; |
| 7565 |
|
7565 |
|
| 7566 |
if (OutputZeroVal) { |
7566 |
if (OutputZeroVal) { |
| 7567 |
if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) { |
7567 |
if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) { |
| 7568 |
HasMismatchedZeros = true; |
7568 |
HasMismatchedZeros = true; |
| 7569 |
CmpLHS = OutputZeroVal; |
7569 |
CmpLHS = OutputZeroVal; |
| 7570 |
} |
7570 |
} |
| 7571 |
if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) { |
7571 |
if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) { |
| 7572 |
HasMismatchedZeros = true; |
7572 |
HasMismatchedZeros = true; |
| 7573 |
CmpRHS = OutputZeroVal; |
7573 |
CmpRHS = OutputZeroVal; |
| 7574 |
} |
7574 |
} |
| 7575 |
} |
7575 |
} |
| 7576 |
} |
7576 |
} |
| 7577 |
|
7577 |
|
| 7578 |
LHS = CmpLHS; |
7578 |
LHS = CmpLHS; |
| 7579 |
RHS = CmpRHS; |
7579 |
RHS = CmpRHS; |
| 7580 |
|
7580 |
|
| 7581 |
// Signed zero may return inconsistent results between implementations. |
7581 |
// Signed zero may return inconsistent results between implementations. |
| 7582 |
// (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 |
7582 |
// (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 |
| 7583 |
// minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) |
7583 |
// minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) |
| 7584 |
// Therefore, we behave conservatively and only proceed if at least one of the |
7584 |
// Therefore, we behave conservatively and only proceed if at least one of the |
| 7585 |
// operands is known to not be zero or if we don't care about signed zero. |
7585 |
// operands is known to not be zero or if we don't care about signed zero. |
| 7586 |
switch (Pred) { |
7586 |
switch (Pred) { |
| 7587 |
default: break; |
7587 |
default: break; |
| 7588 |
case CmpInst::FCMP_OGT: case CmpInst::FCMP_OLT: |
7588 |
case CmpInst::FCMP_OGT: case CmpInst::FCMP_OLT: |
| 7589 |
case CmpInst::FCMP_UGT: case CmpInst::FCMP_ULT: |
7589 |
case CmpInst::FCMP_UGT: case CmpInst::FCMP_ULT: |
| 7590 |
if (!HasMismatchedZeros) |
7590 |
if (!HasMismatchedZeros) |
| 7591 |
break; |
7591 |
break; |
| 7592 |
[[fallthrough]]; |
7592 |
[[fallthrough]]; |
| 7593 |
case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE: |
7593 |
case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE: |
| 7594 |
case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE: |
7594 |
case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE: |
| 7595 |
if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
7595 |
if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
| 7596 |
!isKnownNonZero(CmpRHS)) |
7596 |
!isKnownNonZero(CmpRHS)) |
| 7597 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7597 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7598 |
} |
7598 |
} |
| 7599 |
|
7599 |
|
| 7600 |
SelectPatternNaNBehavior NaNBehavior = SPNB_NA; |
7600 |
SelectPatternNaNBehavior NaNBehavior = SPNB_NA; |
| 7601 |
bool Ordered = false; |
7601 |
bool Ordered = false; |
| 7602 |
|
7602 |
|
| 7603 |
// When given one NaN and one non-NaN input: |
7603 |
// When given one NaN and one non-NaN input: |
| 7604 |
// - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. |
7604 |
// - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. |
| 7605 |
// - A simple C99 (a < b ? a : b) construction will return 'b' (as the |
7605 |
// - A simple C99 (a < b ? a : b) construction will return 'b' (as the |
| 7606 |
// ordered comparison fails), which could be NaN or non-NaN. |
7606 |
// ordered comparison fails), which could be NaN or non-NaN. |
| 7607 |
// so here we discover exactly what NaN behavior is required/accepted. |
7607 |
// so here we discover exactly what NaN behavior is required/accepted. |
| 7608 |
if (CmpInst::isFPPredicate(Pred)) { |
7608 |
if (CmpInst::isFPPredicate(Pred)) { |
| 7609 |
bool LHSSafe = isKnownNonNaN(CmpLHS, FMF); |
7609 |
bool LHSSafe = isKnownNonNaN(CmpLHS, FMF); |
| 7610 |
bool RHSSafe = isKnownNonNaN(CmpRHS, FMF); |
7610 |
bool RHSSafe = isKnownNonNaN(CmpRHS, FMF); |
| 7611 |
|
7611 |
|
| 7612 |
if (LHSSafe && RHSSafe) { |
7612 |
if (LHSSafe && RHSSafe) { |
| 7613 |
// Both operands are known non-NaN. |
7613 |
// Both operands are known non-NaN. |
| 7614 |
NaNBehavior = SPNB_RETURNS_ANY; |
7614 |
NaNBehavior = SPNB_RETURNS_ANY; |
| 7615 |
} else if (CmpInst::isOrdered(Pred)) { |
7615 |
} else if (CmpInst::isOrdered(Pred)) { |
| 7616 |
// An ordered comparison will return false when given a NaN, so it |
7616 |
// An ordered comparison will return false when given a NaN, so it |
| 7617 |
// returns the RHS. |
7617 |
// returns the RHS. |
| 7618 |
Ordered = true; |
7618 |
Ordered = true; |
| 7619 |
if (LHSSafe) |
7619 |
if (LHSSafe) |
| 7620 |
// LHS is non-NaN, so if RHS is NaN then NaN will be returned. |
7620 |
// LHS is non-NaN, so if RHS is NaN then NaN will be returned. |
| 7621 |
NaNBehavior = SPNB_RETURNS_NAN; |
7621 |
NaNBehavior = SPNB_RETURNS_NAN; |
| 7622 |
else if (RHSSafe) |
7622 |
else if (RHSSafe) |
| 7623 |
NaNBehavior = SPNB_RETURNS_OTHER; |
7623 |
NaNBehavior = SPNB_RETURNS_OTHER; |
| 7624 |
else |
7624 |
else |
| 7625 |
// Completely unsafe. |
7625 |
// Completely unsafe. |
| 7626 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7626 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7627 |
} else { |
7627 |
} else { |
| 7628 |
Ordered = false; |
7628 |
Ordered = false; |
| 7629 |
// An unordered comparison will return true when given a NaN, so it |
7629 |
// An unordered comparison will return true when given a NaN, so it |
| 7630 |
// returns the LHS. |
7630 |
// returns the LHS. |
| 7631 |
if (LHSSafe) |
7631 |
if (LHSSafe) |
| 7632 |
// LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. |
7632 |
// LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. |
| 7633 |
NaNBehavior = SPNB_RETURNS_OTHER; |
7633 |
NaNBehavior = SPNB_RETURNS_OTHER; |
| 7634 |
else if (RHSSafe) |
7634 |
else if (RHSSafe) |
| 7635 |
NaNBehavior = SPNB_RETURNS_NAN; |
7635 |
NaNBehavior = SPNB_RETURNS_NAN; |
| 7636 |
else |
7636 |
else |
| 7637 |
// Completely unsafe. |
7637 |
// Completely unsafe. |
| 7638 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7638 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7639 |
} |
7639 |
} |
| 7640 |
} |
7640 |
} |
| 7641 |
|
7641 |
|
| 7642 |
if (TrueVal == CmpRHS && FalseVal == CmpLHS) { |
7642 |
if (TrueVal == CmpRHS && FalseVal == CmpLHS) { |
| 7643 |
std::swap(CmpLHS, CmpRHS); |
7643 |
std::swap(CmpLHS, CmpRHS); |
| 7644 |
Pred = CmpInst::getSwappedPredicate(Pred); |
7644 |
Pred = CmpInst::getSwappedPredicate(Pred); |
| 7645 |
if (NaNBehavior == SPNB_RETURNS_NAN) |
7645 |
if (NaNBehavior == SPNB_RETURNS_NAN) |
| 7646 |
NaNBehavior = SPNB_RETURNS_OTHER; |
7646 |
NaNBehavior = SPNB_RETURNS_OTHER; |
| 7647 |
else if (NaNBehavior == SPNB_RETURNS_OTHER) |
7647 |
else if (NaNBehavior == SPNB_RETURNS_OTHER) |
| 7648 |
NaNBehavior = SPNB_RETURNS_NAN; |
7648 |
NaNBehavior = SPNB_RETURNS_NAN; |
| 7649 |
Ordered = !Ordered; |
7649 |
Ordered = !Ordered; |
| 7650 |
} |
7650 |
} |
| 7651 |
|
7651 |
|
| 7652 |
// ([if]cmp X, Y) ? X : Y |
7652 |
// ([if]cmp X, Y) ? X : Y |
| 7653 |
if (TrueVal == CmpLHS && FalseVal == CmpRHS) { |
7653 |
if (TrueVal == CmpLHS && FalseVal == CmpRHS) { |
| 7654 |
switch (Pred) { |
7654 |
switch (Pred) { |
| 7655 |
default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality. |
7655 |
default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality. |
| 7656 |
case ICmpInst::ICMP_UGT: |
7656 |
case ICmpInst::ICMP_UGT: |
| 7657 |
case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false}; |
7657 |
case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false}; |
| 7658 |
case ICmpInst::ICMP_SGT: |
7658 |
case ICmpInst::ICMP_SGT: |
| 7659 |
case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false}; |
7659 |
case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false}; |
| 7660 |
case ICmpInst::ICMP_ULT: |
7660 |
case ICmpInst::ICMP_ULT: |
| 7661 |
case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false}; |
7661 |
case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false}; |
| 7662 |
case ICmpInst::ICMP_SLT: |
7662 |
case ICmpInst::ICMP_SLT: |
| 7663 |
case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false}; |
7663 |
case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false}; |
| 7664 |
case FCmpInst::FCMP_UGT: |
7664 |
case FCmpInst::FCMP_UGT: |
| 7665 |
case FCmpInst::FCMP_UGE: |
7665 |
case FCmpInst::FCMP_UGE: |
| 7666 |
case FCmpInst::FCMP_OGT: |
7666 |
case FCmpInst::FCMP_OGT: |
| 7667 |
case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered}; |
7667 |
case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered}; |
| 7668 |
case FCmpInst::FCMP_ULT: |
7668 |
case FCmpInst::FCMP_ULT: |
| 7669 |
case FCmpInst::FCMP_ULE: |
7669 |
case FCmpInst::FCMP_ULE: |
| 7670 |
case FCmpInst::FCMP_OLT: |
7670 |
case FCmpInst::FCMP_OLT: |
| 7671 |
case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered}; |
7671 |
case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered}; |
| 7672 |
} |
7672 |
} |
| 7673 |
} |
7673 |
} |
| 7674 |
|
7674 |
|
| 7675 |
if (isKnownNegation(TrueVal, FalseVal)) { |
7675 |
if (isKnownNegation(TrueVal, FalseVal)) { |
| 7676 |
// Sign-extending LHS does not change its sign, so TrueVal/FalseVal can |
7676 |
// Sign-extending LHS does not change its sign, so TrueVal/FalseVal can |
| 7677 |
// match against either LHS or sext(LHS). |
7677 |
// match against either LHS or sext(LHS). |
| 7678 |
auto MaybeSExtCmpLHS = |
7678 |
auto MaybeSExtCmpLHS = |
| 7679 |
m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS))); |
7679 |
m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS))); |
| 7680 |
auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes()); |
7680 |
auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes()); |
| 7681 |
auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One()); |
7681 |
auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One()); |
| 7682 |
if (match(TrueVal, MaybeSExtCmpLHS)) { |
7682 |
if (match(TrueVal, MaybeSExtCmpLHS)) { |
| 7683 |
// Set the return values. If the compare uses the negated value (-X >s 0), |
7683 |
// Set the return values. If the compare uses the negated value (-X >s 0), |
| 7684 |
// swap the return values because the negated value is always 'RHS'. |
7684 |
// swap the return values because the negated value is always 'RHS'. |
| 7685 |
LHS = TrueVal; |
7685 |
LHS = TrueVal; |
| 7686 |
RHS = FalseVal; |
7686 |
RHS = FalseVal; |
| 7687 |
if (match(CmpLHS, m_Neg(m_Specific(FalseVal)))) |
7687 |
if (match(CmpLHS, m_Neg(m_Specific(FalseVal)))) |
| 7688 |
std::swap(LHS, RHS); |
7688 |
std::swap(LHS, RHS); |
| 7689 |
|
7689 |
|
| 7690 |
// (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) |
7690 |
// (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) |
| 7691 |
// (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X) |
7691 |
// (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X) |
| 7692 |
if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) |
7692 |
if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) |
| 7693 |
return {SPF_ABS, SPNB_NA, false}; |
7693 |
return {SPF_ABS, SPNB_NA, false}; |
| 7694 |
|
7694 |
|
| 7695 |
// (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X) |
7695 |
// (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X) |
| 7696 |
if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne)) |
7696 |
if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne)) |
| 7697 |
return {SPF_ABS, SPNB_NA, false}; |
7697 |
return {SPF_ABS, SPNB_NA, false}; |
| 7698 |
|
7698 |
|
| 7699 |
// (X NABS(X) |
7699 |
// (X NABS(X) |
| 7700 |
// (-X NABS(X) |
7700 |
// (-X NABS(X) |
| 7701 |
if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) |
7701 |
if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) |
| 7702 |
return {SPF_NABS, SPNB_NA, false}; |
7702 |
return {SPF_NABS, SPNB_NA, false}; |
| 7703 |
} |
7703 |
} |
| 7704 |
else if (match(FalseVal, MaybeSExtCmpLHS)) { |
7704 |
else if (match(FalseVal, MaybeSExtCmpLHS)) { |
| 7705 |
// Set the return values. If the compare uses the negated value (-X >s 0), |
7705 |
// Set the return values. If the compare uses the negated value (-X >s 0), |
| 7706 |
// swap the return values because the negated value is always 'RHS'. |
7706 |
// swap the return values because the negated value is always 'RHS'. |
| 7707 |
LHS = FalseVal; |
7707 |
LHS = FalseVal; |
| 7708 |
RHS = TrueVal; |
7708 |
RHS = TrueVal; |
| 7709 |
if (match(CmpLHS, m_Neg(m_Specific(TrueVal)))) |
7709 |
if (match(CmpLHS, m_Neg(m_Specific(TrueVal)))) |
| 7710 |
std::swap(LHS, RHS); |
7710 |
std::swap(LHS, RHS); |
| 7711 |
|
7711 |
|
| 7712 |
// (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) |
7712 |
// (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) |
| 7713 |
// (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X) |
7713 |
// (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X) |
| 7714 |
if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) |
7714 |
if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) |
| 7715 |
return {SPF_NABS, SPNB_NA, false}; |
7715 |
return {SPF_NABS, SPNB_NA, false}; |
| 7716 |
|
7716 |
|
| 7717 |
// (X ABS(X) |
7717 |
// (X ABS(X) |
| 7718 |
// (-X ABS(X) |
7718 |
// (-X ABS(X) |
| 7719 |
if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) |
7719 |
if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) |
| 7720 |
return {SPF_ABS, SPNB_NA, false}; |
7720 |
return {SPF_ABS, SPNB_NA, false}; |
| 7721 |
} |
7721 |
} |
| 7722 |
} |
7722 |
} |
| 7723 |
|
7723 |
|
| 7724 |
if (CmpInst::isIntPredicate(Pred)) |
7724 |
if (CmpInst::isIntPredicate(Pred)) |
| 7725 |
return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth); |
7725 |
return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth); |
| 7726 |
|
7726 |
|
| 7727 |
// According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar |
7727 |
// According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar |
| 7728 |
// may return either -0.0 or 0.0, so fcmp/select pair has stricter |
7728 |
// may return either -0.0 or 0.0, so fcmp/select pair has stricter |
| 7729 |
// semantics than minNum. Be conservative in such case. |
7729 |
// semantics than minNum. Be conservative in such case. |
| 7730 |
if (NaNBehavior != SPNB_RETURNS_ANY || |
7730 |
if (NaNBehavior != SPNB_RETURNS_ANY || |
| 7731 |
(!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
7731 |
(!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
| 7732 |
!isKnownNonZero(CmpRHS))) |
7732 |
!isKnownNonZero(CmpRHS))) |
| 7733 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7733 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7734 |
|
7734 |
|
| 7735 |
return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS); |
7735 |
return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS); |
| 7736 |
} |
7736 |
} |
| 7737 |
|
7737 |
|
| 7738 |
/// Helps to match a select pattern in case of a type mismatch. |
7738 |
/// Helps to match a select pattern in case of a type mismatch. |
| 7739 |
/// |
7739 |
/// |
| 7740 |
/// The function processes the case when type of true and false values of a |
7740 |
/// The function processes the case when type of true and false values of a |
| 7741 |
/// select instruction differs from type of the cmp instruction operands because |
7741 |
/// select instruction differs from type of the cmp instruction operands because |
| 7742 |
/// of a cast instruction. The function checks if it is legal to move the cast |
7742 |
/// of a cast instruction. The function checks if it is legal to move the cast |
| 7743 |
/// operation after "select". If yes, it returns the new second value of |
7743 |
/// operation after "select". If yes, it returns the new second value of |
| 7744 |
/// "select" (with the assumption that cast is moved): |
7744 |
/// "select" (with the assumption that cast is moved): |
| 7745 |
/// 1. As operand of cast instruction when both values of "select" are same cast |
7745 |
/// 1. As operand of cast instruction when both values of "select" are same cast |
| 7746 |
/// instructions. |
7746 |
/// instructions. |
| 7747 |
/// 2. As restored constant (by applying reverse cast operation) when the first |
7747 |
/// 2. As restored constant (by applying reverse cast operation) when the first |
| 7748 |
/// value of the "select" is a cast operation and the second value is a |
7748 |
/// value of the "select" is a cast operation and the second value is a |
| 7749 |
/// constant. |
7749 |
/// constant. |
| 7750 |
/// NOTE: We return only the new second value because the first value could be |
7750 |
/// NOTE: We return only the new second value because the first value could be |
| 7751 |
/// accessed as operand of cast instruction. |
7751 |
/// accessed as operand of cast instruction. |
| 7752 |
static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, |
7752 |
static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, |
| 7753 |
Instruction::CastOps *CastOp) { |
7753 |
Instruction::CastOps *CastOp) { |
| 7754 |
auto *Cast1 = dyn_cast(V1); |
7754 |
auto *Cast1 = dyn_cast(V1); |
| 7755 |
if (!Cast1) |
7755 |
if (!Cast1) |
| 7756 |
return nullptr; |
7756 |
return nullptr; |
| 7757 |
|
7757 |
|
| 7758 |
*CastOp = Cast1->getOpcode(); |
7758 |
*CastOp = Cast1->getOpcode(); |
| 7759 |
Type *SrcTy = Cast1->getSrcTy(); |
7759 |
Type *SrcTy = Cast1->getSrcTy(); |
| 7760 |
if (auto *Cast2 = dyn_cast(V2)) { |
7760 |
if (auto *Cast2 = dyn_cast(V2)) { |
| 7761 |
// If V1 and V2 are both the same cast from the same type, look through V1. |
7761 |
// If V1 and V2 are both the same cast from the same type, look through V1. |
| 7762 |
if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy()) |
7762 |
if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy()) |
| 7763 |
return Cast2->getOperand(0); |
7763 |
return Cast2->getOperand(0); |
| 7764 |
return nullptr; |
7764 |
return nullptr; |
| 7765 |
} |
7765 |
} |
| 7766 |
|
7766 |
|
| 7767 |
auto *C = dyn_cast(V2); |
7767 |
auto *C = dyn_cast(V2); |
| 7768 |
if (!C) |
7768 |
if (!C) |
| 7769 |
return nullptr; |
7769 |
return nullptr; |
| 7770 |
|
7770 |
|
| 7771 |
Constant *CastedTo = nullptr; |
7771 |
Constant *CastedTo = nullptr; |
| 7772 |
switch (*CastOp) { |
7772 |
switch (*CastOp) { |
| 7773 |
case Instruction::ZExt: |
7773 |
case Instruction::ZExt: |
| 7774 |
if (CmpI->isUnsigned()) |
7774 |
if (CmpI->isUnsigned()) |
| 7775 |
CastedTo = ConstantExpr::getTrunc(C, SrcTy); |
7775 |
CastedTo = ConstantExpr::getTrunc(C, SrcTy); |
| 7776 |
break; |
7776 |
break; |
| 7777 |
case Instruction::SExt: |
7777 |
case Instruction::SExt: |
| 7778 |
if (CmpI->isSigned()) |
7778 |
if (CmpI->isSigned()) |
| 7779 |
CastedTo = ConstantExpr::getTrunc(C, SrcTy, true); |
7779 |
CastedTo = ConstantExpr::getTrunc(C, SrcTy, true); |
| 7780 |
break; |
7780 |
break; |
| 7781 |
case Instruction::Trunc: |
7781 |
case Instruction::Trunc: |
| 7782 |
Constant *CmpConst; |
7782 |
Constant *CmpConst; |
| 7783 |
if (match(CmpI->getOperand(1), m_Constant(CmpConst)) && |
7783 |
if (match(CmpI->getOperand(1), m_Constant(CmpConst)) && |
| 7784 |
CmpConst->getType() == SrcTy) { |
7784 |
CmpConst->getType() == SrcTy) { |
| 7785 |
// Here we have the following case: |
7785 |
// Here we have the following case: |
| 7786 |
// |
7786 |
// |
| 7787 |
// %cond = cmp iN %x, CmpConst |
7787 |
// %cond = cmp iN %x, CmpConst |
| 7788 |
// %tr = trunc iN %x to iK |
7788 |
// %tr = trunc iN %x to iK |
| 7789 |
// %narrowsel = select i1 %cond, iK %t, iK C |
7789 |
// %narrowsel = select i1 %cond, iK %t, iK C |
| 7790 |
// |
7790 |
// |
| 7791 |
// We can always move trunc after select operation: |
7791 |
// We can always move trunc after select operation: |
| 7792 |
// |
7792 |
// |
| 7793 |
// %cond = cmp iN %x, CmpConst |
7793 |
// %cond = cmp iN %x, CmpConst |
| 7794 |
// %widesel = select i1 %cond, iN %x, iN CmpConst |
7794 |
// %widesel = select i1 %cond, iN %x, iN CmpConst |
| 7795 |
// %tr = trunc iN %widesel to iK |
7795 |
// %tr = trunc iN %widesel to iK |
| 7796 |
// |
7796 |
// |
| 7797 |
// Note that C could be extended in any way because we don't care about |
7797 |
// Note that C could be extended in any way because we don't care about |
| 7798 |
// upper bits after truncation. It can't be abs pattern, because it would |
7798 |
// upper bits after truncation. It can't be abs pattern, because it would |
| 7799 |
// look like: |
7799 |
// look like: |
| 7800 |
// |
7800 |
// |
| 7801 |
// select i1 %cond, x, -x. |
7801 |
// select i1 %cond, x, -x. |
| 7802 |
// |
7802 |
// |
| 7803 |
// So only min/max pattern could be matched. Such match requires widened C |
7803 |
// So only min/max pattern could be matched. Such match requires widened C |
| 7804 |
// == CmpConst. That is why set widened C = CmpConst, condition trunc |
7804 |
// == CmpConst. That is why set widened C = CmpConst, condition trunc |
| 7805 |
// CmpConst == C is checked below. |
7805 |
// CmpConst == C is checked below. |
| 7806 |
CastedTo = CmpConst; |
7806 |
CastedTo = CmpConst; |
| 7807 |
} else { |
7807 |
} else { |
| 7808 |
CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned()); |
7808 |
CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned()); |
| 7809 |
} |
7809 |
} |
| 7810 |
break; |
7810 |
break; |
| 7811 |
case Instruction::FPTrunc: |
7811 |
case Instruction::FPTrunc: |
| 7812 |
CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true); |
7812 |
CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true); |
| 7813 |
break; |
7813 |
break; |
| 7814 |
case Instruction::FPExt: |
7814 |
case Instruction::FPExt: |
| 7815 |
CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true); |
7815 |
CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true); |
| 7816 |
break; |
7816 |
break; |
| 7817 |
case Instruction::FPToUI: |
7817 |
case Instruction::FPToUI: |
| 7818 |
CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true); |
7818 |
CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true); |
| 7819 |
break; |
7819 |
break; |
| 7820 |
case Instruction::FPToSI: |
7820 |
case Instruction::FPToSI: |
| 7821 |
CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true); |
7821 |
CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true); |
| 7822 |
break; |
7822 |
break; |
| 7823 |
case Instruction::UIToFP: |
7823 |
case Instruction::UIToFP: |
| 7824 |
CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true); |
7824 |
CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true); |
| 7825 |
break; |
7825 |
break; |
| 7826 |
case Instruction::SIToFP: |
7826 |
case Instruction::SIToFP: |
| 7827 |
CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true); |
7827 |
CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true); |
| 7828 |
break; |
7828 |
break; |
| 7829 |
default: |
7829 |
default: |
| 7830 |
break; |
7830 |
break; |
| 7831 |
} |
7831 |
} |
| 7832 |
|
7832 |
|
| 7833 |
if (!CastedTo) |
7833 |
if (!CastedTo) |
| 7834 |
return nullptr; |
7834 |
return nullptr; |
| 7835 |
|
7835 |
|
| 7836 |
// Make sure the cast doesn't lose any information. |
7836 |
// Make sure the cast doesn't lose any information. |
| 7837 |
Constant *CastedBack = |
7837 |
Constant *CastedBack = |
| 7838 |
ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true); |
7838 |
ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true); |
| 7839 |
if (CastedBack != C) |
7839 |
if (CastedBack != C) |
| 7840 |
return nullptr; |
7840 |
return nullptr; |
| 7841 |
|
7841 |
|
| 7842 |
return CastedTo; |
7842 |
return CastedTo; |
| 7843 |
} |
7843 |
} |
| 7844 |
|
7844 |
|
| 7845 |
SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, |
7845 |
SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, |
| 7846 |
Instruction::CastOps *CastOp, |
7846 |
Instruction::CastOps *CastOp, |
| 7847 |
unsigned Depth) { |
7847 |
unsigned Depth) { |
| 7848 |
if (Depth >= MaxAnalysisRecursionDepth) |
7848 |
if (Depth >= MaxAnalysisRecursionDepth) |
| 7849 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7849 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7850 |
|
7850 |
|
| 7851 |
SelectInst *SI = dyn_cast(V); |
7851 |
SelectInst *SI = dyn_cast(V); |
| 7852 |
if (!SI) return {SPF_UNKNOWN, SPNB_NA, false}; |
7852 |
if (!SI) return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7853 |
|
7853 |
|
| 7854 |
CmpInst *CmpI = dyn_cast(SI->getCondition()); |
7854 |
CmpInst *CmpI = dyn_cast(SI->getCondition()); |
| 7855 |
if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false}; |
7855 |
if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7856 |
|
7856 |
|
| 7857 |
Value *TrueVal = SI->getTrueValue(); |
7857 |
Value *TrueVal = SI->getTrueValue(); |
| 7858 |
Value *FalseVal = SI->getFalseValue(); |
7858 |
Value *FalseVal = SI->getFalseValue(); |
| 7859 |
|
7859 |
|
| 7860 |
return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS, |
7860 |
return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS, |
| 7861 |
CastOp, Depth); |
7861 |
CastOp, Depth); |
| 7862 |
} |
7862 |
} |
| 7863 |
|
7863 |
|
| 7864 |
SelectPatternResult llvm::matchDecomposedSelectPattern( |
7864 |
SelectPatternResult llvm::matchDecomposedSelectPattern( |
| 7865 |
CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, |
7865 |
CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, |
| 7866 |
Instruction::CastOps *CastOp, unsigned Depth) { |
7866 |
Instruction::CastOps *CastOp, unsigned Depth) { |
| 7867 |
CmpInst::Predicate Pred = CmpI->getPredicate(); |
7867 |
CmpInst::Predicate Pred = CmpI->getPredicate(); |
| 7868 |
Value *CmpLHS = CmpI->getOperand(0); |
7868 |
Value *CmpLHS = CmpI->getOperand(0); |
| 7869 |
Value *CmpRHS = CmpI->getOperand(1); |
7869 |
Value *CmpRHS = CmpI->getOperand(1); |
| 7870 |
FastMathFlags FMF; |
7870 |
FastMathFlags FMF; |
| 7871 |
if (isa(CmpI)) |
7871 |
if (isa(CmpI)) |
| 7872 |
FMF = CmpI->getFastMathFlags(); |
7872 |
FMF = CmpI->getFastMathFlags(); |
| 7873 |
|
7873 |
|
| 7874 |
// Bail out early. |
7874 |
// Bail out early. |
| 7875 |
if (CmpI->isEquality()) |
7875 |
if (CmpI->isEquality()) |
| 7876 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
7876 |
return {SPF_UNKNOWN, SPNB_NA, false}; |
| 7877 |
|
7877 |
|
| 7878 |
// Deal with type mismatches. |
7878 |
// Deal with type mismatches. |
| 7879 |
if (CastOp && CmpLHS->getType() != TrueVal->getType()) { |
7879 |
if (CastOp && CmpLHS->getType() != TrueVal->getType()) { |
| 7880 |
if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) { |
7880 |
if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) { |
| 7881 |
// If this is a potential fmin/fmax with a cast to integer, then ignore |
7881 |
// If this is a potential fmin/fmax with a cast to integer, then ignore |
| 7882 |
// -0.0 because there is no corresponding integer value. |
7882 |
// -0.0 because there is no corresponding integer value. |
| 7883 |
if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) |
7883 |
if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) |
| 7884 |
FMF.setNoSignedZeros(); |
7884 |
FMF.setNoSignedZeros(); |
| 7885 |
return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
7885 |
return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
| 7886 |
cast(TrueVal)->getOperand(0), C, |
7886 |
cast(TrueVal)->getOperand(0), C, |
| 7887 |
LHS, RHS, Depth); |
7887 |
LHS, RHS, Depth); |
| 7888 |
} |
7888 |
} |
| 7889 |
if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) { |
7889 |
if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) { |
| 7890 |
// If this is a potential fmin/fmax with a cast to integer, then ignore |
7890 |
// If this is a potential fmin/fmax with a cast to integer, then ignore |
| 7891 |
// -0.0 because there is no corresponding integer value. |
7891 |
// -0.0 because there is no corresponding integer value. |
| 7892 |
if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) |
7892 |
if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) |
| 7893 |
FMF.setNoSignedZeros(); |
7893 |
FMF.setNoSignedZeros(); |
| 7894 |
return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
7894 |
return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
| 7895 |
C, cast(FalseVal)->getOperand(0), |
7895 |
C, cast(FalseVal)->getOperand(0), |
| 7896 |
LHS, RHS, Depth); |
7896 |
LHS, RHS, Depth); |
| 7897 |
} |
7897 |
} |
| 7898 |
} |
7898 |
} |
| 7899 |
return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, |
7899 |
return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, |
| 7900 |
LHS, RHS, Depth); |
7900 |
LHS, RHS, Depth); |
| 7901 |
} |
7901 |
} |
| 7902 |
|
7902 |
|
| 7903 |
CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) { |
7903 |
CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) { |
| 7904 |
if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT; |
7904 |
if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT; |
| 7905 |
if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT; |
7905 |
if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT; |
| 7906 |
if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT; |
7906 |
if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT; |
| 7907 |
if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT; |
7907 |
if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT; |
| 7908 |
if (SPF == SPF_FMINNUM) |
7908 |
if (SPF == SPF_FMINNUM) |
| 7909 |
return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; |
7909 |
return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; |
| 7910 |
if (SPF == SPF_FMAXNUM) |
7910 |
if (SPF == SPF_FMAXNUM) |
| 7911 |
return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; |
7911 |
return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; |
| 7912 |
llvm_unreachable("unhandled!"); |
7912 |
llvm_unreachable("unhandled!"); |
| 7913 |
} |
7913 |
} |
| 7914 |
|
7914 |
|
| 7915 |
SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) { |
7915 |
SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) { |
| 7916 |
if (SPF == SPF_SMIN) return SPF_SMAX; |
7916 |
if (SPF == SPF_SMIN) return SPF_SMAX; |
| 7917 |
if (SPF == SPF_UMIN) return SPF_UMAX; |
7917 |
if (SPF == SPF_UMIN) return SPF_UMAX; |
| 7918 |
if (SPF == SPF_SMAX) return SPF_SMIN; |
7918 |
if (SPF == SPF_SMAX) return SPF_SMIN; |
| 7919 |
if (SPF == SPF_UMAX) return SPF_UMIN; |
7919 |
if (SPF == SPF_UMAX) return SPF_UMIN; |
| 7920 |
llvm_unreachable("unhandled!"); |
7920 |
llvm_unreachable("unhandled!"); |
| 7921 |
} |
7921 |
} |
| 7922 |
|
7922 |
|
| 7923 |
Intrinsic::ID llvm::getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID) { |
7923 |
Intrinsic::ID llvm::getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID) { |
| 7924 |
switch (MinMaxID) { |
7924 |
switch (MinMaxID) { |
| 7925 |
case Intrinsic::smax: return Intrinsic::smin; |
7925 |
case Intrinsic::smax: return Intrinsic::smin; |
| 7926 |
case Intrinsic::smin: return Intrinsic::smax; |
7926 |
case Intrinsic::smin: return Intrinsic::smax; |
| 7927 |
case Intrinsic::umax: return Intrinsic::umin; |
7927 |
case Intrinsic::umax: return Intrinsic::umin; |
| 7928 |
case Intrinsic::umin: return Intrinsic::umax; |
7928 |
case Intrinsic::umin: return Intrinsic::umax; |
| 7929 |
// Please note that next four intrinsics may produce the same result for |
7929 |
// Please note that next four intrinsics may produce the same result for |
| 7930 |
// original and inverted case even if X != Y due to NaN is handled specially. |
7930 |
// original and inverted case even if X != Y due to NaN is handled specially. |
| 7931 |
case Intrinsic::maximum: return Intrinsic::minimum; |
7931 |
case Intrinsic::maximum: return Intrinsic::minimum; |
| 7932 |
case Intrinsic::minimum: return Intrinsic::maximum; |
7932 |
case Intrinsic::minimum: return Intrinsic::maximum; |
| 7933 |
case Intrinsic::maxnum: return Intrinsic::minnum; |
7933 |
case Intrinsic::maxnum: return Intrinsic::minnum; |
| 7934 |
case Intrinsic::minnum: return Intrinsic::maxnum; |
7934 |
case Intrinsic::minnum: return Intrinsic::maxnum; |
| 7935 |
default: llvm_unreachable("Unexpected intrinsic"); |
7935 |
default: llvm_unreachable("Unexpected intrinsic"); |
| 7936 |
} |
7936 |
} |
| 7937 |
} |
7937 |
} |
| 7938 |
|
7938 |
|
| 7939 |
APInt llvm::getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth) { |
7939 |
APInt llvm::getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth) { |
| 7940 |
switch (SPF) { |
7940 |
switch (SPF) { |
| 7941 |
case SPF_SMAX: return APInt::getSignedMaxValue(BitWidth); |
7941 |
case SPF_SMAX: return APInt::getSignedMaxValue(BitWidth); |
| 7942 |
case SPF_SMIN: return APInt::getSignedMinValue(BitWidth); |
7942 |
case SPF_SMIN: return APInt::getSignedMinValue(BitWidth); |
| 7943 |
case SPF_UMAX: return APInt::getMaxValue(BitWidth); |
7943 |
case SPF_UMAX: return APInt::getMaxValue(BitWidth); |
| 7944 |
case SPF_UMIN: return APInt::getMinValue(BitWidth); |
7944 |
case SPF_UMIN: return APInt::getMinValue(BitWidth); |
| 7945 |
default: llvm_unreachable("Unexpected flavor"); |
7945 |
default: llvm_unreachable("Unexpected flavor"); |
| 7946 |
} |
7946 |
} |
| 7947 |
} |
7947 |
} |
| 7948 |
|
7948 |
|
| 7949 |
std::pair |
7949 |
std::pair |
| 7950 |
llvm::canConvertToMinOrMaxIntrinsic(ArrayRef VL) { |
7950 |
llvm::canConvertToMinOrMaxIntrinsic(ArrayRef VL) { |
| 7951 |
// Check if VL contains select instructions that can be folded into a min/max |
7951 |
// Check if VL contains select instructions that can be folded into a min/max |
| 7952 |
// vector intrinsic and return the intrinsic if it is possible. |
7952 |
// vector intrinsic and return the intrinsic if it is possible. |
| 7953 |
// TODO: Support floating point min/max. |
7953 |
// TODO: Support floating point min/max. |
| 7954 |
bool AllCmpSingleUse = true; |
7954 |
bool AllCmpSingleUse = true; |
| 7955 |
SelectPatternResult SelectPattern; |
7955 |
SelectPatternResult SelectPattern; |
| 7956 |
SelectPattern.Flavor = SPF_UNKNOWN; |
7956 |
SelectPattern.Flavor = SPF_UNKNOWN; |
| 7957 |
if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) { |
7957 |
if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) { |
| 7958 |
Value *LHS, *RHS; |
7958 |
Value *LHS, *RHS; |
| 7959 |
auto CurrentPattern = matchSelectPattern(I, LHS, RHS); |
7959 |
auto CurrentPattern = matchSelectPattern(I, LHS, RHS); |
| 7960 |
if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor) || |
7960 |
if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor) || |
| 7961 |
CurrentPattern.Flavor == SPF_FMINNUM || |
7961 |
CurrentPattern.Flavor == SPF_FMINNUM || |
| 7962 |
CurrentPattern.Flavor == SPF_FMAXNUM || |
7962 |
CurrentPattern.Flavor == SPF_FMAXNUM || |
| 7963 |
!I->getType()->isIntOrIntVectorTy()) |
7963 |
!I->getType()->isIntOrIntVectorTy()) |
| 7964 |
return false; |
7964 |
return false; |
| 7965 |
if (SelectPattern.Flavor != SPF_UNKNOWN && |
7965 |
if (SelectPattern.Flavor != SPF_UNKNOWN && |
| 7966 |
SelectPattern.Flavor != CurrentPattern.Flavor) |
7966 |
SelectPattern.Flavor != CurrentPattern.Flavor) |
| 7967 |
return false; |
7967 |
return false; |
| 7968 |
SelectPattern = CurrentPattern; |
7968 |
SelectPattern = CurrentPattern; |
| 7969 |
AllCmpSingleUse &= |
7969 |
AllCmpSingleUse &= |
| 7970 |
match(I, m_Select(m_OneUse(m_Value()), m_Value(), m_Value())); |
7970 |
match(I, m_Select(m_OneUse(m_Value()), m_Value(), m_Value())); |
| 7971 |
return true; |
7971 |
return true; |
| 7972 |
})) { |
7972 |
})) { |
| 7973 |
switch (SelectPattern.Flavor) { |
7973 |
switch (SelectPattern.Flavor) { |
| 7974 |
case SPF_SMIN: |
7974 |
case SPF_SMIN: |
| 7975 |
return {Intrinsic::smin, AllCmpSingleUse}; |
7975 |
return {Intrinsic::smin, AllCmpSingleUse}; |
| 7976 |
case SPF_UMIN: |
7976 |
case SPF_UMIN: |
| 7977 |
return {Intrinsic::umin, AllCmpSingleUse}; |
7977 |
return {Intrinsic::umin, AllCmpSingleUse}; |
| 7978 |
case SPF_SMAX: |
7978 |
case SPF_SMAX: |
| 7979 |
return {Intrinsic::smax, AllCmpSingleUse}; |
7979 |
return {Intrinsic::smax, AllCmpSingleUse}; |
| 7980 |
case SPF_UMAX: |
7980 |
case SPF_UMAX: |
| 7981 |
return {Intrinsic::umax, AllCmpSingleUse}; |
7981 |
return {Intrinsic::umax, AllCmpSingleUse}; |
| 7982 |
default: |
7982 |
default: |
| 7983 |
llvm_unreachable("unexpected select pattern flavor"); |
7983 |
llvm_unreachable("unexpected select pattern flavor"); |
| 7984 |
} |
7984 |
} |
| 7985 |
} |
7985 |
} |
| 7986 |
return {Intrinsic::not_intrinsic, false}; |
7986 |
return {Intrinsic::not_intrinsic, false}; |
| 7987 |
} |
7987 |
} |
| 7988 |
|
7988 |
|
| 7989 |
bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, |
7989 |
bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, |
| 7990 |
Value *&Start, Value *&Step) { |
7990 |
Value *&Start, Value *&Step) { |
| 7991 |
// Handle the case of a simple two-predecessor recurrence PHI. |
7991 |
// Handle the case of a simple two-predecessor recurrence PHI. |
| 7992 |
// There's a lot more that could theoretically be done here, but |
7992 |
// There's a lot more that could theoretically be done here, but |
| 7993 |
// this is sufficient to catch some interesting cases. |
7993 |
// this is sufficient to catch some interesting cases. |
| 7994 |
if (P->getNumIncomingValues() != 2) |
7994 |
if (P->getNumIncomingValues() != 2) |
| 7995 |
return false; |
7995 |
return false; |
| 7996 |
|
7996 |
|
| 7997 |
for (unsigned i = 0; i != 2; ++i) { |
7997 |
for (unsigned i = 0; i != 2; ++i) { |
| 7998 |
Value *L = P->getIncomingValue(i); |
7998 |
Value *L = P->getIncomingValue(i); |
| 7999 |
Value *R = P->getIncomingValue(!i); |
7999 |
Value *R = P->getIncomingValue(!i); |
| 8000 |
Operator *LU = dyn_cast(L); |
8000 |
Operator *LU = dyn_cast(L); |
| 8001 |
if (!LU) |
8001 |
if (!LU) |
| 8002 |
continue; |
8002 |
continue; |
| 8003 |
unsigned Opcode = LU->getOpcode(); |
8003 |
unsigned Opcode = LU->getOpcode(); |
| 8004 |
|
8004 |
|
| 8005 |
switch (Opcode) { |
8005 |
switch (Opcode) { |
| 8006 |
default: |
8006 |
default: |
| 8007 |
continue; |
8007 |
continue; |
| 8008 |
// TODO: Expand list -- xor, div, gep, uaddo, etc.. |
8008 |
// TODO: Expand list -- xor, div, gep, uaddo, etc.. |
| 8009 |
case Instruction::LShr: |
8009 |
case Instruction::LShr: |
| 8010 |
case Instruction::AShr: |
8010 |
case Instruction::AShr: |
| 8011 |
case Instruction::Shl: |
8011 |
case Instruction::Shl: |
| 8012 |
case Instruction::Add: |
8012 |
case Instruction::Add: |
| 8013 |
case Instruction::Sub: |
8013 |
case Instruction::Sub: |
| 8014 |
case Instruction::And: |
8014 |
case Instruction::And: |
| 8015 |
case Instruction::Or: |
8015 |
case Instruction::Or: |
| 8016 |
case Instruction::Mul: |
8016 |
case Instruction::Mul: |
| 8017 |
case Instruction::FMul: { |
8017 |
case Instruction::FMul: { |
| 8018 |
Value *LL = LU->getOperand(0); |
8018 |
Value *LL = LU->getOperand(0); |
| 8019 |
Value *LR = LU->getOperand(1); |
8019 |
Value *LR = LU->getOperand(1); |
| 8020 |
// Find a recurrence. |
8020 |
// Find a recurrence. |
| 8021 |
if (LL == P) |
8021 |
if (LL == P) |
| 8022 |
L = LR; |
8022 |
L = LR; |
| 8023 |
else if (LR == P) |
8023 |
else if (LR == P) |
| 8024 |
L = LL; |
8024 |
L = LL; |
| 8025 |
else |
8025 |
else |
| 8026 |
continue; // Check for recurrence with L and R flipped. |
8026 |
continue; // Check for recurrence with L and R flipped. |
| 8027 |
|
8027 |
|
| 8028 |
break; // Match! |
8028 |
break; // Match! |
| 8029 |
} |
8029 |
} |
| 8030 |
}; |
8030 |
}; |
| 8031 |
|
8031 |
|
| 8032 |
// We have matched a recurrence of the form: |
8032 |
// We have matched a recurrence of the form: |
| 8033 |
// %iv = [R, %entry], [%iv.next, %backedge] |
8033 |
// %iv = [R, %entry], [%iv.next, %backedge] |
| 8034 |
// %iv.next = binop %iv, L |
8034 |
// %iv.next = binop %iv, L |
| 8035 |
// OR |
8035 |
// OR |
| 8036 |
// %iv = [R, %entry], [%iv.next, %backedge] |
8036 |
// %iv = [R, %entry], [%iv.next, %backedge] |
| 8037 |
// %iv.next = binop L, %iv |
8037 |
// %iv.next = binop L, %iv |
| 8038 |
BO = cast(LU); |
8038 |
BO = cast(LU); |
| 8039 |
Start = R; |
8039 |
Start = R; |
| 8040 |
Step = L; |
8040 |
Step = L; |
| 8041 |
return true; |
8041 |
return true; |
| 8042 |
} |
8042 |
} |
| 8043 |
return false; |
8043 |
return false; |
| 8044 |
} |
8044 |
} |
| 8045 |
|
8045 |
|
| 8046 |
bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P, |
8046 |
bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P, |
| 8047 |
Value *&Start, Value *&Step) { |
8047 |
Value *&Start, Value *&Step) { |
| 8048 |
BinaryOperator *BO = nullptr; |
8048 |
BinaryOperator *BO = nullptr; |
| 8049 |
P = dyn_cast(I->getOperand(0)); |
8049 |
P = dyn_cast(I->getOperand(0)); |
| 8050 |
if (!P) |
8050 |
if (!P) |
| 8051 |
P = dyn_cast(I->getOperand(1)); |
8051 |
P = dyn_cast(I->getOperand(1)); |
| 8052 |
return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I; |
8052 |
return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I; |
| 8053 |
} |
8053 |
} |
| 8054 |
|
8054 |
|
| 8055 |
/// Return true if "icmp Pred LHS RHS" is always true. |
8055 |
/// Return true if "icmp Pred LHS RHS" is always true. |
| 8056 |
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, |
8056 |
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, |
| 8057 |
const Value *RHS, const DataLayout &DL, |
8057 |
const Value *RHS, const DataLayout &DL, |
| 8058 |
unsigned Depth) { |
8058 |
unsigned Depth) { |
| 8059 |
if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS) |
8059 |
if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS) |
| 8060 |
return true; |
8060 |
return true; |
| 8061 |
|
8061 |
|
| 8062 |
switch (Pred) { |
8062 |
switch (Pred) { |
| 8063 |
default: |
8063 |
default: |
| 8064 |
return false; |
8064 |
return false; |
| 8065 |
|
8065 |
|
| 8066 |
case CmpInst::ICMP_SLE: { |
8066 |
case CmpInst::ICMP_SLE: { |
| 8067 |
const APInt *C; |
8067 |
const APInt *C; |
| 8068 |
|
8068 |
|
| 8069 |
// LHS s<= LHS +_{nsw} C if C >= 0 |
8069 |
// LHS s<= LHS +_{nsw} C if C >= 0 |
| 8070 |
if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C)))) |
8070 |
if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C)))) |
| 8071 |
return !C->isNegative(); |
8071 |
return !C->isNegative(); |
| 8072 |
return false; |
8072 |
return false; |
| 8073 |
} |
8073 |
} |
| 8074 |
|
8074 |
|
| 8075 |
case CmpInst::ICMP_ULE: { |
8075 |
case CmpInst::ICMP_ULE: { |
| 8076 |
const APInt *C; |
8076 |
const APInt *C; |
| 8077 |
|
8077 |
|
| 8078 |
// LHS u<= LHS +_{nuw} C for any C |
8078 |
// LHS u<= LHS +_{nuw} C for any C |
| 8079 |
if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C)))) |
8079 |
if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C)))) |
| 8080 |
return true; |
8080 |
return true; |
| 8081 |
|
8081 |
|
| 8082 |
// RHS >> V u<= RHS for any V |
8082 |
// RHS >> V u<= RHS for any V |
| 8083 |
if (match(LHS, m_LShr(m_Specific(RHS), m_Value()))) |
8083 |
if (match(LHS, m_LShr(m_Specific(RHS), m_Value()))) |
| 8084 |
return true; |
8084 |
return true; |
| 8085 |
|
8085 |
|
| 8086 |
// Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB) |
8086 |
// Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB) |
| 8087 |
auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B, |
8087 |
auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B, |
| 8088 |
const Value *&X, |
8088 |
const Value *&X, |
| 8089 |
const APInt *&CA, const APInt *&CB) { |
8089 |
const APInt *&CA, const APInt *&CB) { |
| 8090 |
if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) && |
8090 |
if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) && |
| 8091 |
match(B, m_NUWAdd(m_Specific(X), m_APInt(CB)))) |
8091 |
match(B, m_NUWAdd(m_Specific(X), m_APInt(CB)))) |
| 8092 |
return true; |
8092 |
return true; |
| 8093 |
|
8093 |
|
| 8094 |
// If X & C == 0 then (X | C) == X +_{nuw} C |
8094 |
// If X & C == 0 then (X | C) == X +_{nuw} C |
| 8095 |
if (match(A, m_Or(m_Value(X), m_APInt(CA))) && |
8095 |
if (match(A, m_Or(m_Value(X), m_APInt(CA))) && |
| 8096 |
match(B, m_Or(m_Specific(X), m_APInt(CB)))) { |
8096 |
match(B, m_Or(m_Specific(X), m_APInt(CB)))) { |
| 8097 |
KnownBits Known(CA->getBitWidth()); |
8097 |
KnownBits Known(CA->getBitWidth()); |
| 8098 |
computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr, |
8098 |
computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr, |
| 8099 |
/*CxtI*/ nullptr, /*DT*/ nullptr); |
8099 |
/*CxtI*/ nullptr, /*DT*/ nullptr); |
| 8100 |
if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero)) |
8100 |
if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero)) |
| 8101 |
return true; |
8101 |
return true; |
| 8102 |
} |
8102 |
} |
| 8103 |
|
8103 |
|
| 8104 |
return false; |
8104 |
return false; |
| 8105 |
}; |
8105 |
}; |
| 8106 |
|
8106 |
|
| 8107 |
const Value *X; |
8107 |
const Value *X; |
| 8108 |
const APInt *CLHS, *CRHS; |
8108 |
const APInt *CLHS, *CRHS; |
| 8109 |
if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS)) |
8109 |
if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS)) |
| 8110 |
return CLHS->ule(*CRHS); |
8110 |
return CLHS->ule(*CRHS); |
| 8111 |
|
8111 |
|
| 8112 |
return false; |
8112 |
return false; |
| 8113 |
} |
8113 |
} |
| 8114 |
} |
8114 |
} |
| 8115 |
} |
8115 |
} |
| 8116 |
|
8116 |
|
| 8117 |
/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred |
8117 |
/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred |
| 8118 |
/// ALHS ARHS" is true. Otherwise, return std::nullopt. |
8118 |
/// ALHS ARHS" is true. Otherwise, return std::nullopt. |
| 8119 |
static std::optional |
8119 |
static std::optional |
| 8120 |
isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, |
8120 |
isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, |
| 8121 |
const Value *ARHS, const Value *BLHS, const Value *BRHS, |
8121 |
const Value *ARHS, const Value *BLHS, const Value *BRHS, |
| 8122 |
const DataLayout &DL, unsigned Depth) { |
8122 |
const DataLayout &DL, unsigned Depth) { |
| 8123 |
switch (Pred) { |
8123 |
switch (Pred) { |
| 8124 |
default: |
8124 |
default: |
| 8125 |
return std::nullopt; |
8125 |
return std::nullopt; |
| 8126 |
|
8126 |
|
| 8127 |
case CmpInst::ICMP_SLT: |
8127 |
case CmpInst::ICMP_SLT: |
| 8128 |
case CmpInst::ICMP_SLE: |
8128 |
case CmpInst::ICMP_SLE: |
| 8129 |
if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) && |
8129 |
if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) && |
| 8130 |
isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth)) |
8130 |
isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth)) |
| 8131 |
return true; |
8131 |
return true; |
| 8132 |
return std::nullopt; |
8132 |
return std::nullopt; |
| 8133 |
|
8133 |
|
| 8134 |
case CmpInst::ICMP_SGT: |
8134 |
case CmpInst::ICMP_SGT: |
| 8135 |
case CmpInst::ICMP_SGE: |
8135 |
case CmpInst::ICMP_SGE: |
| 8136 |
if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS, DL, Depth) && |
8136 |
if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS, DL, Depth) && |
| 8137 |
isTruePredicate(CmpInst::ICMP_SLE, BRHS, ARHS, DL, Depth)) |
8137 |
isTruePredicate(CmpInst::ICMP_SLE, BRHS, ARHS, DL, Depth)) |
| 8138 |
return true; |
8138 |
return true; |
| 8139 |
return std::nullopt; |
8139 |
return std::nullopt; |
| 8140 |
|
8140 |
|
| 8141 |
case CmpInst::ICMP_ULT: |
8141 |
case CmpInst::ICMP_ULT: |
| 8142 |
case CmpInst::ICMP_ULE: |
8142 |
case CmpInst::ICMP_ULE: |
| 8143 |
if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) && |
8143 |
if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) && |
| 8144 |
isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth)) |
8144 |
isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth)) |
| 8145 |
return true; |
8145 |
return true; |
| 8146 |
return std::nullopt; |
8146 |
return std::nullopt; |
| 8147 |
|
8147 |
|
| 8148 |
case CmpInst::ICMP_UGT: |
8148 |
case CmpInst::ICMP_UGT: |
| 8149 |
case CmpInst::ICMP_UGE: |
8149 |
case CmpInst::ICMP_UGE: |
| 8150 |
if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS, DL, Depth) && |
8150 |
if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS, DL, Depth) && |
| 8151 |
isTruePredicate(CmpInst::ICMP_ULE, BRHS, ARHS, DL, Depth)) |
8151 |
isTruePredicate(CmpInst::ICMP_ULE, BRHS, ARHS, DL, Depth)) |
| 8152 |
return true; |
8152 |
return true; |
| 8153 |
return std::nullopt; |
8153 |
return std::nullopt; |
| 8154 |
} |
8154 |
} |
| 8155 |
} |
8155 |
} |
| 8156 |
|
8156 |
|
| 8157 |
/// Return true if the operands of two compares (expanded as "L0 pred L1" and |
8157 |
/// Return true if the operands of two compares (expanded as "L0 pred L1" and |
| 8158 |
/// "R0 pred R1") match. IsSwappedOps is true when the operands match, but are |
8158 |
/// "R0 pred R1") match. IsSwappedOps is true when the operands match, but are |
| 8159 |
/// swapped. |
8159 |
/// swapped. |
| 8160 |
static bool areMatchingOperands(const Value *L0, const Value *L1, const Value *R0, |
8160 |
static bool areMatchingOperands(const Value *L0, const Value *L1, const Value *R0, |
| 8161 |
const Value *R1, bool &AreSwappedOps) { |
8161 |
const Value *R1, bool &AreSwappedOps) { |
| 8162 |
bool AreMatchingOps = (L0 == R0 && L1 == R1); |
8162 |
bool AreMatchingOps = (L0 == R0 && L1 == R1); |
| 8163 |
AreSwappedOps = (L0 == R1 && L1 == R0); |
8163 |
AreSwappedOps = (L0 == R1 && L1 == R0); |
| 8164 |
return AreMatchingOps || AreSwappedOps; |
8164 |
return AreMatchingOps || AreSwappedOps; |
| 8165 |
} |
8165 |
} |
| 8166 |
|
8166 |
|
| 8167 |
/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true. |
8167 |
/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true. |
| 8168 |
/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false. |
8168 |
/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false. |
| 8169 |
/// Otherwise, return std::nullopt if we can't infer anything. |
8169 |
/// Otherwise, return std::nullopt if we can't infer anything. |
| 8170 |
static std::optional |
8170 |
static std::optional |
| 8171 |
isImpliedCondMatchingOperands(CmpInst::Predicate LPred, |
8171 |
isImpliedCondMatchingOperands(CmpInst::Predicate LPred, |
| 8172 |
CmpInst::Predicate RPred, bool AreSwappedOps) { |
8172 |
CmpInst::Predicate RPred, bool AreSwappedOps) { |
| 8173 |
// Canonicalize the predicate as if the operands were not commuted. |
8173 |
// Canonicalize the predicate as if the operands were not commuted. |
| 8174 |
if (AreSwappedOps) |
8174 |
if (AreSwappedOps) |
| 8175 |
RPred = ICmpInst::getSwappedPredicate(RPred); |
8175 |
RPred = ICmpInst::getSwappedPredicate(RPred); |
| 8176 |
|
8176 |
|
| 8177 |
if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred)) |
8177 |
if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred)) |
| 8178 |
return true; |
8178 |
return true; |
| 8179 |
if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred)) |
8179 |
if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred)) |
| 8180 |
return false; |
8180 |
return false; |
| 8181 |
|
8181 |
|
| 8182 |
return std::nullopt; |
8182 |
return std::nullopt; |
| 8183 |
} |
8183 |
} |
| 8184 |
|
8184 |
|
| 8185 |
/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true. |
8185 |
/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true. |
| 8186 |
/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false. |
8186 |
/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false. |
| 8187 |
/// Otherwise, return std::nullopt if we can't infer anything. |
8187 |
/// Otherwise, return std::nullopt if we can't infer anything. |
| 8188 |
static std::optional isImpliedCondCommonOperandWithConstants( |
8188 |
static std::optional isImpliedCondCommonOperandWithConstants( |
| 8189 |
CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred, |
8189 |
CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred, |
| 8190 |
const APInt &RC) { |
8190 |
const APInt &RC) { |
| 8191 |
ConstantRange DomCR = ConstantRange::makeExactICmpRegion(LPred, LC); |
8191 |
ConstantRange DomCR = ConstantRange::makeExactICmpRegion(LPred, LC); |
| 8192 |
ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, RC); |
8192 |
ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, RC); |
| 8193 |
ConstantRange Intersection = DomCR.intersectWith(CR); |
8193 |
ConstantRange Intersection = DomCR.intersectWith(CR); |
| 8194 |
ConstantRange Difference = DomCR.difference(CR); |
8194 |
ConstantRange Difference = DomCR.difference(CR); |
| 8195 |
if (Intersection.isEmptySet()) |
8195 |
if (Intersection.isEmptySet()) |
| 8196 |
return false; |
8196 |
return false; |
| 8197 |
if (Difference.isEmptySet()) |
8197 |
if (Difference.isEmptySet()) |
| 8198 |
return true; |
8198 |
return true; |
| 8199 |
return std::nullopt; |
8199 |
return std::nullopt; |
| 8200 |
} |
8200 |
} |
| 8201 |
|
8201 |
|
| 8202 |
/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") |
8202 |
/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") |
| 8203 |
/// is true. Return false if LHS implies RHS is false. Otherwise, return |
8203 |
/// is true. Return false if LHS implies RHS is false. Otherwise, return |
| 8204 |
/// std::nullopt if we can't infer anything. |
8204 |
/// std::nullopt if we can't infer anything. |
| 8205 |
static std::optional isImpliedCondICmps(const ICmpInst *LHS, |
8205 |
static std::optional isImpliedCondICmps(const ICmpInst *LHS, |
| 8206 |
CmpInst::Predicate RPred, |
8206 |
CmpInst::Predicate RPred, |
| 8207 |
const Value *R0, const Value *R1, |
8207 |
const Value *R0, const Value *R1, |
| 8208 |
const DataLayout &DL, |
8208 |
const DataLayout &DL, |
| 8209 |
bool LHSIsTrue, unsigned Depth) { |
8209 |
bool LHSIsTrue, unsigned Depth) { |
| 8210 |
Value *L0 = LHS->getOperand(0); |
8210 |
Value *L0 = LHS->getOperand(0); |
| 8211 |
Value *L1 = LHS->getOperand(1); |
8211 |
Value *L1 = LHS->getOperand(1); |
| 8212 |
|
8212 |
|
| 8213 |
// The rest of the logic assumes the LHS condition is true. If that's not the |
8213 |
// The rest of the logic assumes the LHS condition is true. If that's not the |
| 8214 |
// case, invert the predicate to make it so. |
8214 |
// case, invert the predicate to make it so. |
| 8215 |
CmpInst::Predicate LPred = |
8215 |
CmpInst::Predicate LPred = |
| 8216 |
LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate(); |
8216 |
LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate(); |
| 8217 |
|
8217 |
|
| 8218 |
// Can we infer anything when the two compares have matching operands? |
8218 |
// Can we infer anything when the two compares have matching operands? |
| 8219 |
bool AreSwappedOps; |
8219 |
bool AreSwappedOps; |
| 8220 |
if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps)) |
8220 |
if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps)) |
| 8221 |
return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps); |
8221 |
return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps); |
| 8222 |
|
8222 |
|
| 8223 |
// Can we infer anything when the 0-operands match and the 1-operands are |
8223 |
// Can we infer anything when the 0-operands match and the 1-operands are |
| 8224 |
// constants (not necessarily matching)? |
8224 |
// constants (not necessarily matching)? |
| 8225 |
const APInt *LC, *RC; |
8225 |
const APInt *LC, *RC; |
| 8226 |
if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC))) |
8226 |
if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC))) |
| 8227 |
return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC); |
8227 |
return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC); |
| 8228 |
|
8228 |
|
| 8229 |
if (LPred == RPred) |
8229 |
if (LPred == RPred) |
| 8230 |
return isImpliedCondOperands(LPred, L0, L1, R0, R1, DL, Depth); |
8230 |
return isImpliedCondOperands(LPred, L0, L1, R0, R1, DL, Depth); |
| 8231 |
|
8231 |
|
| 8232 |
return std::nullopt; |
8232 |
return std::nullopt; |
| 8233 |
} |
8233 |
} |
| 8234 |
|
8234 |
|
| 8235 |
/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is |
8235 |
/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is |
| 8236 |
/// false. Otherwise, return std::nullopt if we can't infer anything. We |
8236 |
/// false. Otherwise, return std::nullopt if we can't infer anything. We |
| 8237 |
/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select' |
8237 |
/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select' |
| 8238 |
/// instruction. |
8238 |
/// instruction. |
| 8239 |
static std::optional |
8239 |
static std::optional |
| 8240 |
isImpliedCondAndOr(const Instruction *LHS, CmpInst::Predicate RHSPred, |
8240 |
isImpliedCondAndOr(const Instruction *LHS, CmpInst::Predicate RHSPred, |
| 8241 |
const Value *RHSOp0, const Value *RHSOp1, |
8241 |
const Value *RHSOp0, const Value *RHSOp1, |
| 8242 |
const DataLayout &DL, bool LHSIsTrue, unsigned Depth) { |
8242 |
const DataLayout &DL, bool LHSIsTrue, unsigned Depth) { |
| 8243 |
// The LHS must be an 'or', 'and', or a 'select' instruction. |
8243 |
// The LHS must be an 'or', 'and', or a 'select' instruction. |
| 8244 |
assert((LHS->getOpcode() == Instruction::And || |
8244 |
assert((LHS->getOpcode() == Instruction::And || |
| 8245 |
LHS->getOpcode() == Instruction::Or || |
8245 |
LHS->getOpcode() == Instruction::Or || |
| 8246 |
LHS->getOpcode() == Instruction::Select) && |
8246 |
LHS->getOpcode() == Instruction::Select) && |
| 8247 |
"Expected LHS to be 'and', 'or', or 'select'."); |
8247 |
"Expected LHS to be 'and', 'or', or 'select'."); |
| 8248 |
|
8248 |
|
| 8249 |
assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit"); |
8249 |
assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit"); |
| 8250 |
|
8250 |
|
| 8251 |
// If the result of an 'or' is false, then we know both legs of the 'or' are |
8251 |
// If the result of an 'or' is false, then we know both legs of the 'or' are |
| 8252 |
// false. Similarly, if the result of an 'and' is true, then we know both |
8252 |
// false. Similarly, if the result of an 'and' is true, then we know both |
| 8253 |
// legs of the 'and' are true. |
8253 |
// legs of the 'and' are true. |
| 8254 |
const Value *ALHS, *ARHS; |
8254 |
const Value *ALHS, *ARHS; |
| 8255 |
if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) || |
8255 |
if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) || |
| 8256 |
(LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) { |
8256 |
(LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) { |
| 8257 |
// FIXME: Make this non-recursion. |
8257 |
// FIXME: Make this non-recursion. |
| 8258 |
if (std::optional Implication = isImpliedCondition( |
8258 |
if (std::optional Implication = isImpliedCondition( |
| 8259 |
ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1)) |
8259 |
ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1)) |
| 8260 |
return Implication; |
8260 |
return Implication; |
| 8261 |
if (std::optional Implication = isImpliedCondition( |
8261 |
if (std::optional Implication = isImpliedCondition( |
| 8262 |
ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1)) |
8262 |
ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1)) |
| 8263 |
return Implication; |
8263 |
return Implication; |
| 8264 |
return std::nullopt; |
8264 |
return std::nullopt; |
| 8265 |
} |
8265 |
} |
| 8266 |
return std::nullopt; |
8266 |
return std::nullopt; |
| 8267 |
} |
8267 |
} |
| 8268 |
|
8268 |
|
| 8269 |
std::optional |
8269 |
std::optional |
| 8270 |
llvm::isImpliedCondition(const Value *LHS, CmpInst::Predicate RHSPred, |
8270 |
llvm::isImpliedCondition(const Value *LHS, CmpInst::Predicate RHSPred, |
| 8271 |
const Value *RHSOp0, const Value *RHSOp1, |
8271 |
const Value *RHSOp0, const Value *RHSOp1, |
| 8272 |
const DataLayout &DL, bool LHSIsTrue, unsigned Depth) { |
8272 |
const DataLayout &DL, bool LHSIsTrue, unsigned Depth) { |
| 8273 |
// Bail out when we hit the limit. |
8273 |
// Bail out when we hit the limit. |
| 8274 |
if (Depth == MaxAnalysisRecursionDepth) |
8274 |
if (Depth == MaxAnalysisRecursionDepth) |
| 8275 |
return std::nullopt; |
8275 |
return std::nullopt; |
| 8276 |
|
8276 |
|
| 8277 |
// A mismatch occurs when we compare a scalar cmp to a vector cmp, for |
8277 |
// A mismatch occurs when we compare a scalar cmp to a vector cmp, for |
| 8278 |
// example. |
8278 |
// example. |
| 8279 |
if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy()) |
8279 |
if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy()) |
| 8280 |
return std::nullopt; |
8280 |
return std::nullopt; |
| 8281 |
|
8281 |
|
| 8282 |
assert(LHS->getType()->isIntOrIntVectorTy(1) && |
8282 |
assert(LHS->getType()->isIntOrIntVectorTy(1) && |
| 8283 |
"Expected integer type only!"); |
8283 |
"Expected integer type only!"); |
| 8284 |
|
8284 |
|
| 8285 |
// Both LHS and RHS are icmps. |
8285 |
// Both LHS and RHS are icmps. |
| 8286 |
const ICmpInst *LHSCmp = dyn_cast(LHS); |
8286 |
const ICmpInst *LHSCmp = dyn_cast(LHS); |
| 8287 |
if (LHSCmp) |
8287 |
if (LHSCmp) |
| 8288 |
return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, |
8288 |
return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, |
| 8289 |
Depth); |
8289 |
Depth); |
| 8290 |
|
8290 |
|
| 8291 |
/// The LHS should be an 'or', 'and', or a 'select' instruction. We expect |
8291 |
/// The LHS should be an 'or', 'and', or a 'select' instruction. We expect |
| 8292 |
/// the RHS to be an icmp. |
8292 |
/// the RHS to be an icmp. |
| 8293 |
/// FIXME: Add support for and/or/select on the RHS. |
8293 |
/// FIXME: Add support for and/or/select on the RHS. |
| 8294 |
if (const Instruction *LHSI = dyn_cast(LHS)) { |
8294 |
if (const Instruction *LHSI = dyn_cast(LHS)) { |
| 8295 |
if ((LHSI->getOpcode() == Instruction::And || |
8295 |
if ((LHSI->getOpcode() == Instruction::And || |
| 8296 |
LHSI->getOpcode() == Instruction::Or || |
8296 |
LHSI->getOpcode() == Instruction::Or || |
| 8297 |
LHSI->getOpcode() == Instruction::Select)) |
8297 |
LHSI->getOpcode() == Instruction::Select)) |
| 8298 |
return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, |
8298 |
return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, |
| 8299 |
Depth); |
8299 |
Depth); |
| 8300 |
} |
8300 |
} |
| 8301 |
return std::nullopt; |
8301 |
return std::nullopt; |
| 8302 |
} |
8302 |
} |
| 8303 |
|
8303 |
|
| 8304 |
std::optional llvm::isImpliedCondition(const Value *LHS, const Value *RHS, |
8304 |
std::optional llvm::isImpliedCondition(const Value *LHS, const Value *RHS, |
| 8305 |
const DataLayout &DL, |
8305 |
const DataLayout &DL, |
| 8306 |
bool LHSIsTrue, unsigned Depth) { |
8306 |
bool LHSIsTrue, unsigned Depth) { |
| 8307 |
// LHS ==> RHS by definition |
8307 |
// LHS ==> RHS by definition |
| 8308 |
if (LHS == RHS) |
8308 |
if (LHS == RHS) |
| 8309 |
return LHSIsTrue; |
8309 |
return LHSIsTrue; |
| 8310 |
|
8310 |
|
| 8311 |
if (const ICmpInst *RHSCmp = dyn_cast(RHS)) |
8311 |
if (const ICmpInst *RHSCmp = dyn_cast(RHS)) |
| 8312 |
return isImpliedCondition(LHS, RHSCmp->getPredicate(), |
8312 |
return isImpliedCondition(LHS, RHSCmp->getPredicate(), |
| 8313 |
RHSCmp->getOperand(0), RHSCmp->getOperand(1), DL, |
8313 |
RHSCmp->getOperand(0), RHSCmp->getOperand(1), DL, |
| 8314 |
LHSIsTrue, Depth); |
8314 |
LHSIsTrue, Depth); |
| 8315 |
|
8315 |
|
| 8316 |
if (Depth == MaxAnalysisRecursionDepth) |
8316 |
if (Depth == MaxAnalysisRecursionDepth) |
| 8317 |
return std::nullopt; |
8317 |
return std::nullopt; |
| 8318 |
|
8318 |
|
| 8319 |
// LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2 |
8319 |
// LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2 |
| 8320 |
// LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2 |
8320 |
// LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2 |
| 8321 |
const Value *RHS1, *RHS2; |
8321 |
const Value *RHS1, *RHS2; |
| 8322 |
if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) { |
8322 |
if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) { |
| 8323 |
if (std::optional Imp = |
8323 |
if (std::optional Imp = |
| 8324 |
isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1)) |
8324 |
isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1)) |
| 8325 |
if (*Imp == true) |
8325 |
if (*Imp == true) |
| 8326 |
return true; |
8326 |
return true; |
| 8327 |
if (std::optional Imp = |
8327 |
if (std::optional Imp = |
| 8328 |
isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1)) |
8328 |
isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1)) |
| 8329 |
if (*Imp == true) |
8329 |
if (*Imp == true) |
| 8330 |
return true; |
8330 |
return true; |
| 8331 |
} |
8331 |
} |
| 8332 |
if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) { |
8332 |
if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) { |
| 8333 |
if (std::optional Imp = |
8333 |
if (std::optional Imp = |
| 8334 |
isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1)) |
8334 |
isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1)) |
| 8335 |
if (*Imp == false) |
8335 |
if (*Imp == false) |
| 8336 |
return false; |
8336 |
return false; |
| 8337 |
if (std::optional Imp = |
8337 |
if (std::optional Imp = |
| 8338 |
isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1)) |
8338 |
isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1)) |
| 8339 |
if (*Imp == false) |
8339 |
if (*Imp == false) |
| 8340 |
return false; |
8340 |
return false; |
| 8341 |
} |
8341 |
} |
| 8342 |
|
8342 |
|
| 8343 |
return std::nullopt; |
8343 |
return std::nullopt; |
| 8344 |
} |
8344 |
} |
| 8345 |
|
8345 |
|
| 8346 |
// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch |
8346 |
// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch |
| 8347 |
// condition dominating ContextI or nullptr, if no condition is found. |
8347 |
// condition dominating ContextI or nullptr, if no condition is found. |
| 8348 |
static std::pair |
8348 |
static std::pair |
| 8349 |
getDomPredecessorCondition(const Instruction *ContextI) { |
8349 |
getDomPredecessorCondition(const Instruction *ContextI) { |
| 8350 |
if (!ContextI || !ContextI->getParent()) |
8350 |
if (!ContextI || !ContextI->getParent()) |
| 8351 |
return {nullptr, false}; |
8351 |
return {nullptr, false}; |
| 8352 |
|
8352 |
|
| 8353 |
// TODO: This is a poor/cheap way to determine dominance. Should we use a |
8353 |
// TODO: This is a poor/cheap way to determine dominance. Should we use a |
| 8354 |
// dominator tree (eg, from a SimplifyQuery) instead? |
8354 |
// dominator tree (eg, from a SimplifyQuery) instead? |
| 8355 |
const BasicBlock *ContextBB = ContextI->getParent(); |
8355 |
const BasicBlock *ContextBB = ContextI->getParent(); |
| 8356 |
const BasicBlock *PredBB = ContextBB->getSinglePredecessor(); |
8356 |
const BasicBlock *PredBB = ContextBB->getSinglePredecessor(); |
| 8357 |
if (!PredBB) |
8357 |
if (!PredBB) |
| 8358 |
return {nullptr, false}; |
8358 |
return {nullptr, false}; |
| 8359 |
|
8359 |
|
| 8360 |
// We need a conditional branch in the predecessor. |
8360 |
// We need a conditional branch in the predecessor. |
| 8361 |
Value *PredCond; |
8361 |
Value *PredCond; |
| 8362 |
BasicBlock *TrueBB, *FalseBB; |
8362 |
BasicBlock *TrueBB, *FalseBB; |
| 8363 |
if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB))) |
8363 |
if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB))) |
| 8364 |
return {nullptr, false}; |
8364 |
return {nullptr, false}; |
| 8365 |
|
8365 |
|
| 8366 |
// The branch should get simplified. Don't bother simplifying this condition. |
8366 |
// The branch should get simplified. Don't bother simplifying this condition. |
| 8367 |
if (TrueBB == FalseBB) |
8367 |
if (TrueBB == FalseBB) |
| 8368 |
return {nullptr, false}; |
8368 |
return {nullptr, false}; |
| 8369 |
|
8369 |
|
| 8370 |
assert((TrueBB == ContextBB || FalseBB == ContextBB) && |
8370 |
assert((TrueBB == ContextBB || FalseBB == ContextBB) && |
| 8371 |
"Predecessor block does not point to successor?"); |
8371 |
"Predecessor block does not point to successor?"); |
| 8372 |
|
8372 |
|
| 8373 |
// Is this condition implied by the predecessor condition? |
8373 |
// Is this condition implied by the predecessor condition? |
| 8374 |
return {PredCond, TrueBB == ContextBB}; |
8374 |
return {PredCond, TrueBB == ContextBB}; |
| 8375 |
} |
8375 |
} |
| 8376 |
|
8376 |
|
| 8377 |
std::optional llvm::isImpliedByDomCondition(const Value *Cond, |
8377 |
std::optional llvm::isImpliedByDomCondition(const Value *Cond, |
| 8378 |
const Instruction *ContextI, |
8378 |
const Instruction *ContextI, |
| 8379 |
const DataLayout &DL) { |
8379 |
const DataLayout &DL) { |
| 8380 |
assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool"); |
8380 |
assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool"); |
| 8381 |
auto PredCond = getDomPredecessorCondition(ContextI); |
8381 |
auto PredCond = getDomPredecessorCondition(ContextI); |
| 8382 |
if (PredCond.first) |
8382 |
if (PredCond.first) |
| 8383 |
return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second); |
8383 |
return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second); |
| 8384 |
return std::nullopt; |
8384 |
return std::nullopt; |
| 8385 |
} |
8385 |
} |
| 8386 |
|
8386 |
|
| 8387 |
std::optional llvm::isImpliedByDomCondition(CmpInst::Predicate Pred, |
8387 |
std::optional llvm::isImpliedByDomCondition(CmpInst::Predicate Pred, |
| 8388 |
const Value *LHS, |
8388 |
const Value *LHS, |
| 8389 |
const Value *RHS, |
8389 |
const Value *RHS, |
| 8390 |
const Instruction *ContextI, |
8390 |
const Instruction *ContextI, |
| 8391 |
const DataLayout &DL) { |
8391 |
const DataLayout &DL) { |
| 8392 |
auto PredCond = getDomPredecessorCondition(ContextI); |
8392 |
auto PredCond = getDomPredecessorCondition(ContextI); |
| 8393 |
if (PredCond.first) |
8393 |
if (PredCond.first) |
| 8394 |
return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL, |
8394 |
return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL, |
| 8395 |
PredCond.second); |
8395 |
PredCond.second); |
| 8396 |
return std::nullopt; |
8396 |
return std::nullopt; |
| 8397 |
} |
8397 |
} |
| 8398 |
|
8398 |
|
| 8399 |
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, |
8399 |
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, |
| 8400 |
APInt &Upper, const InstrInfoQuery &IIQ, |
8400 |
APInt &Upper, const InstrInfoQuery &IIQ, |
| 8401 |
bool PreferSignedRange) { |
8401 |
bool PreferSignedRange) { |
| 8402 |
unsigned Width = Lower.getBitWidth(); |
8402 |
unsigned Width = Lower.getBitWidth(); |
| 8403 |
const APInt *C; |
8403 |
const APInt *C; |
| 8404 |
switch (BO.getOpcode()) { |
8404 |
switch (BO.getOpcode()) { |
| 8405 |
case Instruction::Add: |
8405 |
case Instruction::Add: |
| 8406 |
if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) { |
8406 |
if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) { |
| 8407 |
bool HasNSW = IIQ.hasNoSignedWrap(&BO); |
8407 |
bool HasNSW = IIQ.hasNoSignedWrap(&BO); |
| 8408 |
bool HasNUW = IIQ.hasNoUnsignedWrap(&BO); |
8408 |
bool HasNUW = IIQ.hasNoUnsignedWrap(&BO); |
| 8409 |
|
8409 |
|
| 8410 |
// If the caller expects a signed compare, then try to use a signed range. |
8410 |
// If the caller expects a signed compare, then try to use a signed range. |
| 8411 |
// Otherwise if both no-wraps are set, use the unsigned range because it |
8411 |
// Otherwise if both no-wraps are set, use the unsigned range because it |
| 8412 |
// is never larger than the signed range. Example: |
8412 |
// is never larger than the signed range. Example: |
| 8413 |
// "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125]. |
8413 |
// "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125]. |
| 8414 |
if (PreferSignedRange && HasNSW && HasNUW) |
8414 |
if (PreferSignedRange && HasNSW && HasNUW) |
| 8415 |
HasNUW = false; |
8415 |
HasNUW = false; |
| 8416 |
|
8416 |
|
| 8417 |
if (HasNUW) { |
8417 |
if (HasNUW) { |
| 8418 |
// 'add nuw x, C' produces [C, UINT_MAX]. |
8418 |
// 'add nuw x, C' produces [C, UINT_MAX]. |
| 8419 |
Lower = *C; |
8419 |
Lower = *C; |
| 8420 |
} else if (HasNSW) { |
8420 |
} else if (HasNSW) { |
| 8421 |
if (C->isNegative()) { |
8421 |
if (C->isNegative()) { |
| 8422 |
// 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. |
8422 |
// 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. |
| 8423 |
Lower = APInt::getSignedMinValue(Width); |
8423 |
Lower = APInt::getSignedMinValue(Width); |
| 8424 |
Upper = APInt::getSignedMaxValue(Width) + *C + 1; |
8424 |
Upper = APInt::getSignedMaxValue(Width) + *C + 1; |
| 8425 |
} else { |
8425 |
} else { |
| 8426 |
// 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. |
8426 |
// 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. |
| 8427 |
Lower = APInt::getSignedMinValue(Width) + *C; |
8427 |
Lower = APInt::getSignedMinValue(Width) + *C; |
| 8428 |
Upper = APInt::getSignedMaxValue(Width) + 1; |
8428 |
Upper = APInt::getSignedMaxValue(Width) + 1; |
| 8429 |
} |
8429 |
} |
| 8430 |
} |
8430 |
} |
| 8431 |
} |
8431 |
} |
| 8432 |
break; |
8432 |
break; |
| 8433 |
|
8433 |
|
| 8434 |
case Instruction::And: |
8434 |
case Instruction::And: |
| 8435 |
if (match(BO.getOperand(1), m_APInt(C))) |
8435 |
if (match(BO.getOperand(1), m_APInt(C))) |
| 8436 |
// 'and x, C' produces [0, C]. |
8436 |
// 'and x, C' produces [0, C]. |
| 8437 |
Upper = *C + 1; |
8437 |
Upper = *C + 1; |
| 8438 |
break; |
8438 |
break; |
| 8439 |
|
8439 |
|
| 8440 |
case Instruction::Or: |
8440 |
case Instruction::Or: |
| 8441 |
if (match(BO.getOperand(1), m_APInt(C))) |
8441 |
if (match(BO.getOperand(1), m_APInt(C))) |
| 8442 |
// 'or x, C' produces [C, UINT_MAX]. |
8442 |
// 'or x, C' produces [C, UINT_MAX]. |
| 8443 |
Lower = *C; |
8443 |
Lower = *C; |
| 8444 |
break; |
8444 |
break; |
| 8445 |
|
8445 |
|
| 8446 |
case Instruction::AShr: |
8446 |
case Instruction::AShr: |
| 8447 |
if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
8447 |
if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 8448 |
// 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. |
8448 |
// 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. |
| 8449 |
Lower = APInt::getSignedMinValue(Width).ashr(*C); |
8449 |
Lower = APInt::getSignedMinValue(Width).ashr(*C); |
| 8450 |
Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1; |
8450 |
Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1; |
| 8451 |
} else if (match(BO.getOperand(0), m_APInt(C))) { |
8451 |
} else if (match(BO.getOperand(0), m_APInt(C))) { |
| 8452 |
unsigned ShiftAmount = Width - 1; |
8452 |
unsigned ShiftAmount = Width - 1; |
| 8453 |
if (!C->isZero() && IIQ.isExact(&BO)) |
8453 |
if (!C->isZero() && IIQ.isExact(&BO)) |
| 8454 |
ShiftAmount = C->countr_zero(); |
8454 |
ShiftAmount = C->countr_zero(); |
| 8455 |
if (C->isNegative()) { |
8455 |
if (C->isNegative()) { |
| 8456 |
// 'ashr C, x' produces [C, C >> (Width-1)] |
8456 |
// 'ashr C, x' produces [C, C >> (Width-1)] |
| 8457 |
Lower = *C; |
8457 |
Lower = *C; |
| 8458 |
Upper = C->ashr(ShiftAmount) + 1; |
8458 |
Upper = C->ashr(ShiftAmount) + 1; |
| 8459 |
} else { |
8459 |
} else { |
| 8460 |
// 'ashr C, x' produces [C >> (Width-1), C] |
8460 |
// 'ashr C, x' produces [C >> (Width-1), C] |
| 8461 |
Lower = C->ashr(ShiftAmount); |
8461 |
Lower = C->ashr(ShiftAmount); |
| 8462 |
Upper = *C + 1; |
8462 |
Upper = *C + 1; |
| 8463 |
} |
8463 |
} |
| 8464 |
} |
8464 |
} |
| 8465 |
break; |
8465 |
break; |
| 8466 |
|
8466 |
|
| 8467 |
case Instruction::LShr: |
8467 |
case Instruction::LShr: |
| 8468 |
if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
8468 |
if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) { |
| 8469 |
// 'lshr x, C' produces [0, UINT_MAX >> C]. |
8469 |
// 'lshr x, C' produces [0, UINT_MAX >> C]. |
| 8470 |
Upper = APInt::getAllOnes(Width).lshr(*C) + 1; |
8470 |
Upper = APInt::getAllOnes(Width).lshr(*C) + 1; |
| 8471 |
} else if (match(BO.getOperand(0), m_APInt(C))) { |
8471 |
} else if (match(BO.getOperand(0), m_APInt(C))) { |
| 8472 |
// 'lshr C, x' produces [C >> (Width-1), C]. |
8472 |
// 'lshr C, x' produces [C >> (Width-1), C]. |
| 8473 |
unsigned ShiftAmount = Width - 1; |
8473 |
unsigned ShiftAmount = Width - 1; |
| 8474 |
if (!C->isZero() && IIQ.isExact(&BO)) |
8474 |
if (!C->isZero() && IIQ.isExact(&BO)) |
| 8475 |
ShiftAmount = C->countr_zero(); |
8475 |
ShiftAmount = C->countr_zero(); |
| 8476 |
Lower = C->lshr(ShiftAmount); |
8476 |
Lower = C->lshr(ShiftAmount); |
| 8477 |
Upper = *C + 1; |
8477 |
Upper = *C + 1; |
| 8478 |
} |
8478 |
} |
| 8479 |
break; |
8479 |
break; |
| 8480 |
|
8480 |
|
| 8481 |
case Instruction::Shl: |
8481 |
case Instruction::Shl: |
| 8482 |
if (match(BO.getOperand(0), m_APInt(C))) { |
8482 |
if (match(BO.getOperand(0), m_APInt(C))) { |
| 8483 |
if (IIQ.hasNoUnsignedWrap(&BO)) { |
8483 |
if (IIQ.hasNoUnsignedWrap(&BO)) { |
| 8484 |
// 'shl nuw C, x' produces [C, C << CLZ(C)] |
8484 |
// 'shl nuw C, x' produces [C, C << CLZ(C)] |
| 8485 |
Lower = *C; |
8485 |
Lower = *C; |
| 8486 |
Upper = Lower.shl(Lower.countl_zero()) + 1; |
8486 |
Upper = Lower.shl(Lower.countl_zero()) + 1; |
| 8487 |
} else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw? |
8487 |
} else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw? |
| 8488 |
if (C->isNegative()) { |
8488 |
if (C->isNegative()) { |
| 8489 |
// 'shl nsw C, x' produces [C << CLO(C)-1, C] |
8489 |
// 'shl nsw C, x' produces [C << CLO(C)-1, C] |
| 8490 |
unsigned ShiftAmount = C->countl_one() - 1; |
8490 |
unsigned ShiftAmount = C->countl_one() - 1; |
| 8491 |
Lower = C->shl(ShiftAmount); |
8491 |
Lower = C->shl(ShiftAmount); |
| 8492 |
Upper = *C + 1; |
8492 |
Upper = *C + 1; |
| 8493 |
} else { |
8493 |
} else { |
| 8494 |
// 'shl nsw C, x' produces [C, C << CLZ(C)-1] |
8494 |
// 'shl nsw C, x' produces [C, C << CLZ(C)-1] |
| 8495 |
unsigned ShiftAmount = C->countl_zero() - 1; |
8495 |
unsigned ShiftAmount = C->countl_zero() - 1; |
| 8496 |
Lower = *C; |
8496 |
Lower = *C; |
| 8497 |
Upper = C->shl(ShiftAmount) + 1; |
8497 |
Upper = C->shl(ShiftAmount) + 1; |
| 8498 |
} |
8498 |
} |
| 8499 |
} |
8499 |
} |
| 8500 |
} |
8500 |
} |
| 8501 |
break; |
8501 |
break; |
| 8502 |
|
8502 |
|
| 8503 |
case Instruction::SDiv: |
8503 |
case Instruction::SDiv: |
| 8504 |
if (match(BO.getOperand(1), m_APInt(C))) { |
8504 |
if (match(BO.getOperand(1), m_APInt(C))) { |
| 8505 |
APInt IntMin = APInt::getSignedMinValue(Width); |
8505 |
APInt IntMin = APInt::getSignedMinValue(Width); |
| 8506 |
APInt IntMax = APInt::getSignedMaxValue(Width); |
8506 |
APInt IntMax = APInt::getSignedMaxValue(Width); |
| 8507 |
if (C->isAllOnes()) { |
8507 |
if (C->isAllOnes()) { |
| 8508 |
// 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
8508 |
// 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] |
| 8509 |
// where C != -1 and C != 0 and C != 1 |
8509 |
// where C != -1 and C != 0 and C != 1 |
| 8510 |
Lower = IntMin + 1; |
8510 |
Lower = IntMin + 1; |
| 8511 |
Upper = IntMax + 1; |
8511 |
Upper = IntMax + 1; |
| 8512 |
} else if (C->countl_zero() < Width - 1) { |
8512 |
} else if (C->countl_zero() < Width - 1) { |
| 8513 |
// 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] |
8513 |
// 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] |
| 8514 |
// where C != -1 and C != 0 and C != 1 |
8514 |
// where C != -1 and C != 0 and C != 1 |
| 8515 |
Lower = IntMin.sdiv(*C); |
8515 |
Lower = IntMin.sdiv(*C); |
| 8516 |
Upper = IntMax.sdiv(*C); |
8516 |
Upper = IntMax.sdiv(*C); |
| 8517 |
if (Lower.sgt(Upper)) |
8517 |
if (Lower.sgt(Upper)) |
| 8518 |
std::swap(Lower, Upper); |
8518 |
std::swap(Lower, Upper); |
| 8519 |
Upper = Upper + 1; |
8519 |
Upper = Upper + 1; |
| 8520 |
assert(Upper != Lower && "Upper part of range has wrapped!"); |
8520 |
assert(Upper != Lower && "Upper part of range has wrapped!"); |
| 8521 |
} |
8521 |
} |
| 8522 |
} else if (match(BO.getOperand(0), m_APInt(C))) { |
8522 |
} else if (match(BO.getOperand(0), m_APInt(C))) { |
| 8523 |
if (C->isMinSignedValue()) { |
8523 |
if (C->isMinSignedValue()) { |
| 8524 |
// 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
8524 |
// 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. |
| 8525 |
Lower = *C; |
8525 |
Lower = *C; |
| 8526 |
Upper = Lower.lshr(1) + 1; |
8526 |
Upper = Lower.lshr(1) + 1; |
| 8527 |
} else { |
8527 |
} else { |
| 8528 |
// 'sdiv C, x' produces [-|C|, |C|]. |
8528 |
// 'sdiv C, x' produces [-|C|, |C|]. |
| 8529 |
Upper = C->abs() + 1; |
8529 |
Upper = C->abs() + 1; |
| 8530 |
Lower = (-Upper) + 1; |
8530 |
Lower = (-Upper) + 1; |
| 8531 |
} |
8531 |
} |
| 8532 |
} |
8532 |
} |
| 8533 |
break; |
8533 |
break; |
| 8534 |
|
8534 |
|
| 8535 |
case Instruction::UDiv: |
8535 |
case Instruction::UDiv: |
| 8536 |
if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) { |
8536 |
if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) { |
| 8537 |
// 'udiv x, C' produces [0, UINT_MAX / C]. |
8537 |
// 'udiv x, C' produces [0, UINT_MAX / C]. |
| 8538 |
Upper = APInt::getMaxValue(Width).udiv(*C) + 1; |
8538 |
Upper = APInt::getMaxValue(Width).udiv(*C) + 1; |
| 8539 |
} else if (match(BO.getOperand(0), m_APInt(C))) { |
8539 |
} else if (match(BO.getOperand(0), m_APInt(C))) { |
| 8540 |
// 'udiv C, x' produces [0, C]. |
8540 |
// 'udiv C, x' produces [0, C]. |
| 8541 |
Upper = *C + 1; |
8541 |
Upper = *C + 1; |
| 8542 |
} |
8542 |
} |
| 8543 |
break; |
8543 |
break; |
| 8544 |
|
8544 |
|
| 8545 |
case Instruction::SRem: |
8545 |
case Instruction::SRem: |
| 8546 |
if (match(BO.getOperand(1), m_APInt(C))) { |
8546 |
if (match(BO.getOperand(1), m_APInt(C))) { |
| 8547 |
// 'srem x, C' produces (-|C|, |C|). |
8547 |
// 'srem x, C' produces (-|C|, |C|). |
| 8548 |
Upper = C->abs(); |
8548 |
Upper = C->abs(); |
| 8549 |
Lower = (-Upper) + 1; |
8549 |
Lower = (-Upper) + 1; |
| 8550 |
} |
8550 |
} |
| 8551 |
break; |
8551 |
break; |
| 8552 |
|
8552 |
|
| 8553 |
case Instruction::URem: |
8553 |
case Instruction::URem: |
| 8554 |
if (match(BO.getOperand(1), m_APInt(C))) |
8554 |
if (match(BO.getOperand(1), m_APInt(C))) |
| 8555 |
// 'urem x, C' produces [0, C). |
8555 |
// 'urem x, C' produces [0, C). |
| 8556 |
Upper = *C; |
8556 |
Upper = *C; |
| 8557 |
break; |
8557 |
break; |
| 8558 |
|
8558 |
|
| 8559 |
default: |
8559 |
default: |
| 8560 |
break; |
8560 |
break; |
| 8561 |
} |
8561 |
} |
| 8562 |
} |
8562 |
} |
| 8563 |
|
8563 |
|
| 8564 |
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) { |
8564 |
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) { |
| 8565 |
unsigned Width = II.getType()->getScalarSizeInBits(); |
8565 |
unsigned Width = II.getType()->getScalarSizeInBits(); |
| 8566 |
const APInt *C; |
8566 |
const APInt *C; |
| 8567 |
switch (II.getIntrinsicID()) { |
8567 |
switch (II.getIntrinsicID()) { |
| 8568 |
case Intrinsic::ctpop: |
8568 |
case Intrinsic::ctpop: |
| 8569 |
case Intrinsic::ctlz: |
8569 |
case Intrinsic::ctlz: |
| 8570 |
case Intrinsic::cttz: |
8570 |
case Intrinsic::cttz: |
| 8571 |
// Maximum of set/clear bits is the bit width. |
8571 |
// Maximum of set/clear bits is the bit width. |
| 8572 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), |
8572 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), |
| 8573 |
APInt(Width, Width + 1)); |
8573 |
APInt(Width, Width + 1)); |
| 8574 |
case Intrinsic::uadd_sat: |
8574 |
case Intrinsic::uadd_sat: |
| 8575 |
// uadd.sat(x, C) produces [C, UINT_MAX]. |
8575 |
// uadd.sat(x, C) produces [C, UINT_MAX]. |
| 8576 |
if (match(II.getOperand(0), m_APInt(C)) || |
8576 |
if (match(II.getOperand(0), m_APInt(C)) || |
| 8577 |
match(II.getOperand(1), m_APInt(C))) |
8577 |
match(II.getOperand(1), m_APInt(C))) |
| 8578 |
return ConstantRange::getNonEmpty(*C, APInt::getZero(Width)); |
8578 |
return ConstantRange::getNonEmpty(*C, APInt::getZero(Width)); |
| 8579 |
break; |
8579 |
break; |
| 8580 |
case Intrinsic::sadd_sat: |
8580 |
case Intrinsic::sadd_sat: |
| 8581 |
if (match(II.getOperand(0), m_APInt(C)) || |
8581 |
if (match(II.getOperand(0), m_APInt(C)) || |
| 8582 |
match(II.getOperand(1), m_APInt(C))) { |
8582 |
match(II.getOperand(1), m_APInt(C))) { |
| 8583 |
if (C->isNegative()) |
8583 |
if (C->isNegative()) |
| 8584 |
// sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)]. |
8584 |
// sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)]. |
| 8585 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width), |
8585 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width), |
| 8586 |
APInt::getSignedMaxValue(Width) + *C + |
8586 |
APInt::getSignedMaxValue(Width) + *C + |
| 8587 |
1); |
8587 |
1); |
| 8588 |
|
8588 |
|
| 8589 |
// sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX]. |
8589 |
// sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX]. |
| 8590 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) + *C, |
8590 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) + *C, |
| 8591 |
APInt::getSignedMaxValue(Width) + 1); |
8591 |
APInt::getSignedMaxValue(Width) + 1); |
| 8592 |
} |
8592 |
} |
| 8593 |
break; |
8593 |
break; |
| 8594 |
case Intrinsic::usub_sat: |
8594 |
case Intrinsic::usub_sat: |
| 8595 |
// usub.sat(C, x) produces [0, C]. |
8595 |
// usub.sat(C, x) produces [0, C]. |
| 8596 |
if (match(II.getOperand(0), m_APInt(C))) |
8596 |
if (match(II.getOperand(0), m_APInt(C))) |
| 8597 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1); |
8597 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1); |
| 8598 |
|
8598 |
|
| 8599 |
// usub.sat(x, C) produces [0, UINT_MAX - C]. |
8599 |
// usub.sat(x, C) produces [0, UINT_MAX - C]. |
| 8600 |
if (match(II.getOperand(1), m_APInt(C))) |
8600 |
if (match(II.getOperand(1), m_APInt(C))) |
| 8601 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), |
8601 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), |
| 8602 |
APInt::getMaxValue(Width) - *C + 1); |
8602 |
APInt::getMaxValue(Width) - *C + 1); |
| 8603 |
break; |
8603 |
break; |
| 8604 |
case Intrinsic::ssub_sat: |
8604 |
case Intrinsic::ssub_sat: |
| 8605 |
if (match(II.getOperand(0), m_APInt(C))) { |
8605 |
if (match(II.getOperand(0), m_APInt(C))) { |
| 8606 |
if (C->isNegative()) |
8606 |
if (C->isNegative()) |
| 8607 |
// ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)]. |
8607 |
// ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)]. |
| 8608 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width), |
8608 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width), |
| 8609 |
*C - APInt::getSignedMinValue(Width) + |
8609 |
*C - APInt::getSignedMinValue(Width) + |
| 8610 |
1); |
8610 |
1); |
| 8611 |
|
8611 |
|
| 8612 |
// ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX]. |
8612 |
// ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX]. |
| 8613 |
return ConstantRange::getNonEmpty(*C - APInt::getSignedMaxValue(Width), |
8613 |
return ConstantRange::getNonEmpty(*C - APInt::getSignedMaxValue(Width), |
| 8614 |
APInt::getSignedMaxValue(Width) + 1); |
8614 |
APInt::getSignedMaxValue(Width) + 1); |
| 8615 |
} else if (match(II.getOperand(1), m_APInt(C))) { |
8615 |
} else if (match(II.getOperand(1), m_APInt(C))) { |
| 8616 |
if (C->isNegative()) |
8616 |
if (C->isNegative()) |
| 8617 |
// ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]: |
8617 |
// ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]: |
| 8618 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) - *C, |
8618 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) - *C, |
| 8619 |
APInt::getSignedMaxValue(Width) + 1); |
8619 |
APInt::getSignedMaxValue(Width) + 1); |
| 8620 |
|
8620 |
|
| 8621 |
// ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C]. |
8621 |
// ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C]. |
| 8622 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width), |
8622 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width), |
| 8623 |
APInt::getSignedMaxValue(Width) - *C + |
8623 |
APInt::getSignedMaxValue(Width) - *C + |
| 8624 |
1); |
8624 |
1); |
| 8625 |
} |
8625 |
} |
| 8626 |
break; |
8626 |
break; |
| 8627 |
case Intrinsic::umin: |
8627 |
case Intrinsic::umin: |
| 8628 |
case Intrinsic::umax: |
8628 |
case Intrinsic::umax: |
| 8629 |
case Intrinsic::smin: |
8629 |
case Intrinsic::smin: |
| 8630 |
case Intrinsic::smax: |
8630 |
case Intrinsic::smax: |
| 8631 |
if (!match(II.getOperand(0), m_APInt(C)) && |
8631 |
if (!match(II.getOperand(0), m_APInt(C)) && |
| 8632 |
!match(II.getOperand(1), m_APInt(C))) |
8632 |
!match(II.getOperand(1), m_APInt(C))) |
| 8633 |
break; |
8633 |
break; |
| 8634 |
|
8634 |
|
| 8635 |
switch (II.getIntrinsicID()) { |
8635 |
switch (II.getIntrinsicID()) { |
| 8636 |
case Intrinsic::umin: |
8636 |
case Intrinsic::umin: |
| 8637 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1); |
8637 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1); |
| 8638 |
case Intrinsic::umax: |
8638 |
case Intrinsic::umax: |
| 8639 |
return ConstantRange::getNonEmpty(*C, APInt::getZero(Width)); |
8639 |
return ConstantRange::getNonEmpty(*C, APInt::getZero(Width)); |
| 8640 |
case Intrinsic::smin: |
8640 |
case Intrinsic::smin: |
| 8641 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width), |
8641 |
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width), |
| 8642 |
*C + 1); |
8642 |
*C + 1); |
| 8643 |
case Intrinsic::smax: |
8643 |
case Intrinsic::smax: |
| 8644 |
return ConstantRange::getNonEmpty(*C, |
8644 |
return ConstantRange::getNonEmpty(*C, |
| 8645 |
APInt::getSignedMaxValue(Width) + 1); |
8645 |
APInt::getSignedMaxValue(Width) + 1); |
| 8646 |
default: |
8646 |
default: |
| 8647 |
llvm_unreachable("Must be min/max intrinsic"); |
8647 |
llvm_unreachable("Must be min/max intrinsic"); |
| 8648 |
} |
8648 |
} |
| 8649 |
break; |
8649 |
break; |
| 8650 |
case Intrinsic::abs: |
8650 |
case Intrinsic::abs: |
| 8651 |
// If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX], |
8651 |
// If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX], |
| 8652 |
// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. |
8652 |
// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. |
| 8653 |
if (match(II.getOperand(1), m_One())) |
8653 |
if (match(II.getOperand(1), m_One())) |
| 8654 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), |
8654 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), |
| 8655 |
APInt::getSignedMaxValue(Width) + 1); |
8655 |
APInt::getSignedMaxValue(Width) + 1); |
| 8656 |
|
8656 |
|
| 8657 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), |
8657 |
return ConstantRange::getNonEmpty(APInt::getZero(Width), |
| 8658 |
APInt::getSignedMinValue(Width) + 1); |
8658 |
APInt::getSignedMinValue(Width) + 1); |
| 8659 |
case Intrinsic::vscale: |
8659 |
case Intrinsic::vscale: |
| 8660 |
if (!II.getParent() || !II.getFunction()) |
8660 |
if (!II.getParent() || !II.getFunction()) |
| 8661 |
break; |
8661 |
break; |
| 8662 |
return getVScaleRange(II.getFunction(), Width); |
8662 |
return getVScaleRange(II.getFunction(), Width); |
| 8663 |
default: |
8663 |
default: |
| 8664 |
break; |
8664 |
break; |
| 8665 |
} |
8665 |
} |
| 8666 |
|
8666 |
|
| 8667 |
return ConstantRange::getFull(Width); |
8667 |
return ConstantRange::getFull(Width); |
| 8668 |
} |
8668 |
} |
| 8669 |
|
8669 |
|
| 8670 |
static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower, |
8670 |
static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower, |
| 8671 |
APInt &Upper, const InstrInfoQuery &IIQ) { |
8671 |
APInt &Upper, const InstrInfoQuery &IIQ) { |
| 8672 |
const Value *LHS = nullptr, *RHS = nullptr; |
8672 |
const Value *LHS = nullptr, *RHS = nullptr; |
| 8673 |
SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS); |
8673 |
SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS); |
| 8674 |
if (R.Flavor == SPF_UNKNOWN) |
8674 |
if (R.Flavor == SPF_UNKNOWN) |
| 8675 |
return; |
8675 |
return; |
| 8676 |
|
8676 |
|
| 8677 |
unsigned BitWidth = SI.getType()->getScalarSizeInBits(); |
8677 |
unsigned BitWidth = SI.getType()->getScalarSizeInBits(); |
| 8678 |
|
8678 |
|
| 8679 |
if (R.Flavor == SelectPatternFlavor::SPF_ABS) { |
8679 |
if (R.Flavor == SelectPatternFlavor::SPF_ABS) { |
| 8680 |
// If the negation part of the abs (in RHS) has the NSW flag, |
8680 |
// If the negation part of the abs (in RHS) has the NSW flag, |
| 8681 |
// then the result of abs(X) is [0..SIGNED_MAX], |
8681 |
// then the result of abs(X) is [0..SIGNED_MAX], |
| 8682 |
// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. |
8682 |
// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. |
| 8683 |
Lower = APInt::getZero(BitWidth); |
8683 |
Lower = APInt::getZero(BitWidth); |
| 8684 |
if (match(RHS, m_Neg(m_Specific(LHS))) && |
8684 |
if (match(RHS, m_Neg(m_Specific(LHS))) && |
| 8685 |
IIQ.hasNoSignedWrap(cast(RHS))) |
8685 |
IIQ.hasNoSignedWrap(cast(RHS))) |
| 8686 |
Upper = APInt::getSignedMaxValue(BitWidth) + 1; |
8686 |
Upper = APInt::getSignedMaxValue(BitWidth) + 1; |
| 8687 |
else |
8687 |
else |
| 8688 |
Upper = APInt::getSignedMinValue(BitWidth) + 1; |
8688 |
Upper = APInt::getSignedMinValue(BitWidth) + 1; |
| 8689 |
return; |
8689 |
return; |
| 8690 |
} |
8690 |
} |
| 8691 |
|
8691 |
|
| 8692 |
if (R.Flavor == SelectPatternFlavor::SPF_NABS) { |
8692 |
if (R.Flavor == SelectPatternFlavor::SPF_NABS) { |
| 8693 |
// The result of -abs(X) is <= 0. |
8693 |
// The result of -abs(X) is <= 0. |
| 8694 |
Lower = APInt::getSignedMinValue(BitWidth); |
8694 |
Lower = APInt::getSignedMinValue(BitWidth); |
| 8695 |
Upper = APInt(BitWidth, 1); |
8695 |
Upper = APInt(BitWidth, 1); |
| 8696 |
return; |
8696 |
return; |
| 8697 |
} |
8697 |
} |
| 8698 |
|
8698 |
|
| 8699 |
const APInt *C; |
8699 |
const APInt *C; |
| 8700 |
if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C))) |
8700 |
if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C))) |
| 8701 |
return; |
8701 |
return; |
| 8702 |
|
8702 |
|
| 8703 |
switch (R.Flavor) { |
8703 |
switch (R.Flavor) { |
| 8704 |
case SPF_UMIN: |
8704 |
case SPF_UMIN: |
| 8705 |
Upper = *C + 1; |
8705 |
Upper = *C + 1; |
| 8706 |
break; |
8706 |
break; |
| 8707 |
case SPF_UMAX: |
8707 |
case SPF_UMAX: |
| 8708 |
Lower = *C; |
8708 |
Lower = *C; |
| 8709 |
break; |
8709 |
break; |
| 8710 |
case SPF_SMIN: |
8710 |
case SPF_SMIN: |
| 8711 |
Lower = APInt::getSignedMinValue(BitWidth); |
8711 |
Lower = APInt::getSignedMinValue(BitWidth); |
| 8712 |
Upper = *C + 1; |
8712 |
Upper = *C + 1; |
| 8713 |
break; |
8713 |
break; |
| 8714 |
case SPF_SMAX: |
8714 |
case SPF_SMAX: |
| 8715 |
Lower = *C; |
8715 |
Lower = *C; |
| 8716 |
Upper = APInt::getSignedMaxValue(BitWidth) + 1; |
8716 |
Upper = APInt::getSignedMaxValue(BitWidth) + 1; |
| 8717 |
break; |
8717 |
break; |
| 8718 |
default: |
8718 |
default: |
| 8719 |
break; |
8719 |
break; |
| 8720 |
} |
8720 |
} |
| 8721 |
} |
8721 |
} |
| 8722 |
|
8722 |
|
| 8723 |
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) { |
8723 |
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) { |
| 8724 |
// The maximum representable value of a half is 65504. For floats the maximum |
8724 |
// The maximum representable value of a half is 65504. For floats the maximum |
| 8725 |
// value is 3.4e38 which requires roughly 129 bits. |
8725 |
// value is 3.4e38 which requires roughly 129 bits. |
| 8726 |
unsigned BitWidth = I->getType()->getScalarSizeInBits(); |
8726 |
unsigned BitWidth = I->getType()->getScalarSizeInBits(); |
| 8727 |
if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy()) |
8727 |
if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy()) |
| 8728 |
return; |
8728 |
return; |
| 8729 |
if (isa(I) && BitWidth >= 17) { |
8729 |
if (isa(I) && BitWidth >= 17) { |
| 8730 |
Lower = APInt(BitWidth, -65504); |
8730 |
Lower = APInt(BitWidth, -65504); |
| 8731 |
Upper = APInt(BitWidth, 65505); |
8731 |
Upper = APInt(BitWidth, 65505); |
| 8732 |
} |
8732 |
} |
| 8733 |
|
8733 |
|
| 8734 |
if (isa(I) && BitWidth >= 16) { |
8734 |
if (isa(I) && BitWidth >= 16) { |
| 8735 |
// For a fptoui the lower limit is left as 0. |
8735 |
// For a fptoui the lower limit is left as 0. |
| 8736 |
Upper = APInt(BitWidth, 65505); |
8736 |
Upper = APInt(BitWidth, 65505); |
| 8737 |
} |
8737 |
} |
| 8738 |
} |
8738 |
} |
| 8739 |
|
8739 |
|
| 8740 |
ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned, |
8740 |
ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned, |
| 8741 |
bool UseInstrInfo, AssumptionCache *AC, |
8741 |
bool UseInstrInfo, AssumptionCache *AC, |
| 8742 |
const Instruction *CtxI, |
8742 |
const Instruction *CtxI, |
| 8743 |
const DominatorTree *DT, |
8743 |
const DominatorTree *DT, |
| 8744 |
unsigned Depth) { |
8744 |
unsigned Depth) { |
| 8745 |
assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction"); |
8745 |
assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction"); |
| 8746 |
|
8746 |
|
| 8747 |
if (Depth == MaxAnalysisRecursionDepth) |
8747 |
if (Depth == MaxAnalysisRecursionDepth) |
| 8748 |
return ConstantRange::getFull(V->getType()->getScalarSizeInBits()); |
8748 |
return ConstantRange::getFull(V->getType()->getScalarSizeInBits()); |
| 8749 |
|
8749 |
|
| 8750 |
const APInt *C; |
8750 |
const APInt *C; |
| 8751 |
if (match(V, m_APInt(C))) |
8751 |
if (match(V, m_APInt(C))) |
| 8752 |
return ConstantRange(*C); |
8752 |
return ConstantRange(*C); |
| 8753 |
|
8753 |
|
| 8754 |
InstrInfoQuery IIQ(UseInstrInfo); |
8754 |
InstrInfoQuery IIQ(UseInstrInfo); |
| 8755 |
unsigned BitWidth = V->getType()->getScalarSizeInBits(); |
8755 |
unsigned BitWidth = V->getType()->getScalarSizeInBits(); |
| 8756 |
ConstantRange CR = ConstantRange::getFull(BitWidth); |
8756 |
ConstantRange CR = ConstantRange::getFull(BitWidth); |
| 8757 |
if (auto *BO = dyn_cast(V)) { |
8757 |
if (auto *BO = dyn_cast(V)) { |
| 8758 |
APInt Lower = APInt(BitWidth, 0); |
8758 |
APInt Lower = APInt(BitWidth, 0); |
| 8759 |
APInt Upper = APInt(BitWidth, 0); |
8759 |
APInt Upper = APInt(BitWidth, 0); |
| 8760 |
// TODO: Return ConstantRange. |
8760 |
// TODO: Return ConstantRange. |
| 8761 |
setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned); |
8761 |
setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned); |
| 8762 |
CR = ConstantRange::getNonEmpty(Lower, Upper); |
8762 |
CR = ConstantRange::getNonEmpty(Lower, Upper); |
| 8763 |
} else if (auto *II = dyn_cast(V)) |
8763 |
} else if (auto *II = dyn_cast(V)) |
| 8764 |
CR = getRangeForIntrinsic(*II); |
8764 |
CR = getRangeForIntrinsic(*II); |
| 8765 |
else if (auto *SI = dyn_cast(V)) { |
8765 |
else if (auto *SI = dyn_cast(V)) { |
| 8766 |
APInt Lower = APInt(BitWidth, 0); |
8766 |
APInt Lower = APInt(BitWidth, 0); |
| 8767 |
APInt Upper = APInt(BitWidth, 0); |
8767 |
APInt Upper = APInt(BitWidth, 0); |
| 8768 |
// TODO: Return ConstantRange. |
8768 |
// TODO: Return ConstantRange. |
| 8769 |
setLimitsForSelectPattern(*SI, Lower, Upper, IIQ); |
8769 |
setLimitsForSelectPattern(*SI, Lower, Upper, IIQ); |
| 8770 |
CR = ConstantRange::getNonEmpty(Lower, Upper); |
8770 |
CR = ConstantRange::getNonEmpty(Lower, Upper); |
| 8771 |
} else if (isa(V) || isa(V)) { |
8771 |
} else if (isa(V) || isa(V)) { |
| 8772 |
APInt Lower = APInt(BitWidth, 0); |
8772 |
APInt Lower = APInt(BitWidth, 0); |
| 8773 |
APInt Upper = APInt(BitWidth, 0); |
8773 |
APInt Upper = APInt(BitWidth, 0); |
| 8774 |
// TODO: Return ConstantRange. |
8774 |
// TODO: Return ConstantRange. |
| 8775 |
setLimitForFPToI(cast(V), Lower, Upper); |
8775 |
setLimitForFPToI(cast(V), Lower, Upper); |
| 8776 |
CR = ConstantRange::getNonEmpty(Lower, Upper); |
8776 |
CR = ConstantRange::getNonEmpty(Lower, Upper); |
| 8777 |
} |
8777 |
} |
| 8778 |
|
8778 |
|
| 8779 |
if (auto *I = dyn_cast(V)) |
8779 |
if (auto *I = dyn_cast(V)) |
| 8780 |
if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range)) |
8780 |
if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range)) |
| 8781 |
CR = CR.intersectWith(getConstantRangeFromMetadata(*Range)); |
8781 |
CR = CR.intersectWith(getConstantRangeFromMetadata(*Range)); |
| 8782 |
|
8782 |
|
| 8783 |
if (CtxI && AC) { |
8783 |
if (CtxI && AC) { |
| 8784 |
// Try to restrict the range based on information from assumptions. |
8784 |
// Try to restrict the range based on information from assumptions. |
| 8785 |
for (auto &AssumeVH : AC->assumptionsFor(V)) { |
8785 |
for (auto &AssumeVH : AC->assumptionsFor(V)) { |
| 8786 |
if (!AssumeVH) |
8786 |
if (!AssumeVH) |
| 8787 |
continue; |
8787 |
continue; |
| 8788 |
CallInst *I = cast(AssumeVH); |
8788 |
CallInst *I = cast(AssumeVH); |
| 8789 |
assert(I->getParent()->getParent() == CtxI->getParent()->getParent() && |
8789 |
assert(I->getParent()->getParent() == CtxI->getParent()->getParent() && |
| 8790 |
"Got assumption for the wrong function!"); |
8790 |
"Got assumption for the wrong function!"); |
| 8791 |
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
8791 |
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
| 8792 |
"must be an assume intrinsic"); |
8792 |
"must be an assume intrinsic"); |
| 8793 |
|
8793 |
|
| 8794 |
if (!isValidAssumeForContext(I, CtxI, DT)) |
8794 |
if (!isValidAssumeForContext(I, CtxI, DT)) |
| 8795 |
continue; |
8795 |
continue; |
| 8796 |
Value *Arg = I->getArgOperand(0); |
8796 |
Value *Arg = I->getArgOperand(0); |
| 8797 |
ICmpInst *Cmp = dyn_cast(Arg); |
8797 |
ICmpInst *Cmp = dyn_cast(Arg); |
| 8798 |
// Currently we just use information from comparisons. |
8798 |
// Currently we just use information from comparisons. |
| 8799 |
if (!Cmp || Cmp->getOperand(0) != V) |
8799 |
if (!Cmp || Cmp->getOperand(0) != V) |
| 8800 |
continue; |
8800 |
continue; |
| 8801 |
// TODO: Set "ForSigned" parameter via Cmp->isSigned()? |
8801 |
// TODO: Set "ForSigned" parameter via Cmp->isSigned()? |
| 8802 |
ConstantRange RHS = |
8802 |
ConstantRange RHS = |
| 8803 |
computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false, |
8803 |
computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false, |
| 8804 |
UseInstrInfo, AC, I, DT, Depth + 1); |
8804 |
UseInstrInfo, AC, I, DT, Depth + 1); |
| 8805 |
CR = CR.intersectWith( |
8805 |
CR = CR.intersectWith( |
| 8806 |
ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS)); |
8806 |
ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS)); |
| 8807 |
} |
8807 |
} |
| 8808 |
} |
8808 |
} |
| 8809 |
|
8809 |
|
| 8810 |
return CR; |
8810 |
return CR; |
| 8811 |
} |
8811 |
} |
| 8812 |
|
8812 |
|